![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 11
Rep Power: 0
![]() |
How to save the array vars in a class?
Hi,
I'm using serialize to save an object which worked until I started using Arrays in the class. Please see this code below: class Base extends Tile
{
var $owner;
var $resources = Array();
function Base($owner)
{
$this->Tile();
$this->owner = $owner;
}
function __sleep()
{
// used this before which works for simple vars like owner
//return array('owner');
// trying this now but doesn't seem to work for arrays
return( array_keys( get_object_vars( &$this ) ) );
}
}Then you would just call serialize and unserialize. But like I commented that doesn't work, the array is not saved. Any suggestions what could I try? Thanks. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Can you show your serialize and deserialize code? I have never heard of problems with those functions working with arrays, and the code you have shown me does necessairly indicate anything I would see as an error--yet.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 11
Rep Power: 0
![]() |
Sure kurifu, thanks for looking into this!
first the $map is initialized like this: $map = array(); and filled with new Tile() this is how the map is saved:
saveGlobal('map',$map);
for($y = 0; $y < MAP_HEIGHT; $y += 1)
{
for($x = 0; $x < MAP_WIDTH; $x += 1)
{
$tile = $map[$y][$x];
if(!isset($tile))
exit("tile $x $y not set");
saveGlobal("m$x$y",serialize($tile));
}
}this is how it's loaded:
$map = loadGlobal('map');
for($y = 0; $y < MAP_HEIGHT; $y += 1)
{
for($x = 0; $x < MAP_WIDTH; $x += 1)
{
$s = loadGlobal("m$x$y");
$map[$y][$x] = unserialize($s);
}
}And here are the functions used:
function saveGlobal($varname,$value)
{
$_SESSION[$varname] = $value;
}
function loadGlobal($varname)
{
return $_SESSION[$varname];
}The problem should not be in the session code I think because everything else works except the array saving. Thank you, please understand that I am a beginner at php and more of an C++ coder so please feel free to comment the code, it would help me. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| pointer to array of a class | sackarias | C++ | 2 | Mar 28th, 2006 8:04 PM |
| What is: "Oriented programming (OO)?" | BrinyCode | C++ | 12 | Nov 22nd, 2005 7:40 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| using an array with a inputbox class | gencor45 | C# | 2 | Feb 24th, 2005 4:31 PM |