Jump to content


Photo

How to save the array vars in a class?


  • Please log in to reply
5 replies to this topic

#1 cCj

cCj
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 August 2006 - 01:30 PM

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 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 10 August 2006 - 01:32 PM

What do you mean with "the array is not saved"?
~ D Kuang

#3 cCj

cCj
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 10 August 2006 - 05:09 PM

What do you mean with "the array is not saved"?


It's empty once unserialized. The owner var however has it's value so I was thinking that it was "not saved".

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 August 2006 - 08:21 PM

I don't see any point in your __sleep() function.

If you run the script below it will create a Base object then save it. On subsequnt runs it pickes up the saved object (check the time values)

<?php
session_start();


class Base
{
	 var $owner;
	 var $resources = Array();
	 
	 function Base($owner)
	 {
		$this->owner = $owner;
		$this->resources = getdate();
	 }
}

if (isset($_SESSION['mybase'])) {
	$obj = unserialize($_SESSION['mybase']) ;
} else {
	$obj = new Base(1);
}

echo '<pre>', print_r($obj, true), '</pre>';

$_SESSION['mybase'] = serialize($obj);
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 cCj

cCj
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 August 2006 - 04:52 AM

Thank you Barand, your code works just like mine and I was able to find the problem now that I knew the problem was not in the serialazation.
What I was doing was something like this:

$tile = $map[$y][$x];
$tile->addToUnits($unit);

When the working code was:
$map[$y][$x]->addToUnits($unit);

So I had misunderstood how php handles variables, even $map had objects the assigment to $tile actually copied the object instead of passing reference to it. So I will be using & to get the reference instead of copy from now on.

Thanks.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 August 2006 - 09:13 AM

If you can, move to php5. Its object handling is far superior, and "by ref" is the default instead of passing copies of objects.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users