Re: Storing objects in sessions recursively

From: Date: Mon, 27 Nov 2006 12:08:29 +0000
Subject: Re: Storing objects in sessions recursively
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-245177@lists.php.net to get a copy of this message
Stut wrote: > Dave M G wrote: >> The issue turned out to be my Article class took arguments in its >> constructor, which was unacceptable because the argument could not be >> serialized when being passed along during the session. >> >> By adjusting my object so that the constructor takes no arguments, and >> then creating a new method on the object to do what the constructor >> did previously, the problem was solved. >> >> That explanation may be confusing, but that's because I don't fully >> understand serialization myself yet. All I can say for sure is that >> the problem was definitely a serialization issue. > > When objects are unserialised the constructor will be called without > arguments because the values passed to the constructor are not heh Stut, this is wrong - the ctor is not called at all when unserializing, check this code snippet: php -r ' class Test { function __construct() { echo "foo\n"; } } $t = new Test; $s = serialize($t); unset($t); $u = unserialize($s); ' this only outputs 'foo' once. seems like whatever Dave's problem was it's actually down to missing ctor args. > automatically stored in the object. You can get around that without > having to create a second initialisation method by using default values > for your arguments... > > class Foo > { > public __construct($param = null) > { > if (!is_null($param)) > { > // Parameter passed, do something with it > } > } > } > > -Stut >

« previous php.general (#245177) next »