Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   How to save the array vars in a class? (http://www.programmingforums.org/showthread.php?t=10951)

cCj Aug 4th, 2006 11:27 PM

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.

kurifu Aug 5th, 2006 5:23 PM

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.

cCj Aug 6th, 2006 1:20 AM

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.


All times are GMT -5. The time now is 12:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC