Page 1 of 1
Passing _full_ Objects between pages
Posted: Wed Nov 23, 2005 6:16 am
by foobar
Yea, I know you can use serialize() and put the Object into the session, but that comes out the other end as a struct with the variables in it. I anybody familiar with a way of passing a real, full Object from page to page?
I have a User class that handles all user data with database-to-session persistence. So say, if the user changes something in their profile, it's done through this class, which updates session data as well. Now of course I could just create an instance of the object on every page, but for the sake of principle I'd like to keep a one user-object, one session relationship.
It's really just about me being pedantic and petty, I know, but it would still be neat to implement...
Any ideas?
Posted: Wed Nov 23, 2005 6:23 am
by Jenk
serialize() and unserialize() will perform exactly what you want them to do. Providing your classes are structured correctly.
That a look on php.net regarding the "magic" member-functions __wakeup() and __sleep()
HTH
EDIT: Also for further reading, see here:
http://www.php.net/manual/en/language.o ... zation.php
Posted: Wed Nov 23, 2005 6:49 am
by foobar
@Jenk
PHP Manual wrote:
[...] The functions in an object will not be saved, only the name of the class. [...]
That's exactly the problem...
Concerning __sleep() and __wakeup(), I'm not sure how I should implement this. When I get home I'll do some experimenting with it. The thing is, I would have to create an instance of the object itself in __wakeup() or something. This is starting to sound more and more screwed up, and not very elegant at all.
Thanks for the input, though.
Posted: Wed Nov 23, 2005 7:32 am
by Jenk
In your example, a db class, in the __wakeup() member function, you initialise the connection - just like you would in the constructor. In __sleep() you close it - just like you wouldin the destructor.
When objects are passed between pages, what actually occurs is the class variables (properties) are saved somewhere. On the next page, the object is instantiated again, just like a new object, and the class variables are then assigned their previous values.
just be sure to include the class definition
before you unserialize() the object.
Posted: Wed Nov 23, 2005 8:42 am
by foobar
Jenk wrote:In your example, a db class, in the __wakeup() member function, you initialise the connection - just like you would in the constructor. In __sleep() you close it - just like you wouldin the destructor.
When objects are passed between pages, what actually occurs is the class variables (properties) are saved somewhere. On the next page, the object is instantiated again, just like a new object, and the class variables are then assigned their previous values.
just be sure to include the class definition
before you unserialize() the object.
Hm... sounds like a good idea. You see, I've never done this before, as my coding in PHP has been primarily procedural (albeit avoiding spaghetti-code

). Thanks for the help, Jenk, I appreciate your expertise!

Posted: Wed Nov 23, 2005 9:44 am
by wtf
foobar wrote:@Jenk
PHP Manual wrote:
[...] The functions in an object will not be saved, only the name of the class. [...]
Not sure what manual you're referring to
Posted: Wed Nov 23, 2005 10:16 am
by Skittlewidth
He's refering to the link at the bottom of Jenks post.
When you serialize an object in PHP4+ its true, the functions are not saved. Try serializing one and reading the string. But as it stores the reference to the class when you unserialize the object it recreates itself from the class data you will hopefully have included before running the unserialize command, and you will regain access to those methods, just as Jenk said.
I guess the problem in PHP3 was that the reference to the class was not stored, rendering the whole thing pointless.