Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 4th, 2006, 10:27 PM   #1
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
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.
cCj is offline   Reply With Quote
Old Aug 5th, 2006, 4:23 PM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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
kurifu is offline   Reply With Quote
Old Aug 6th, 2006, 12:20 AM   #3
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
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.
cCj is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:47 PM.

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