Passing Objects through pages

I have 2 questions here…

  1. How do we pass objects through pages? Serialization?
    A simple example to show would be great :slight_smile:

  2. If we decide to use session to store the objects in pageA, do we still need to include the class used in pageB?

Thanks alot :smiley:

While you could serialize an object to a string, and then have the client pass this data back(urls, forms, cookies etc…) you then need to worry about the string being manipulated, so a lot of validation is required.

Putting them into a sessions would alleviate this. Just be aware that you’re now making that object global to every page the users browser has open, by putting it in the session. Sometimes you want per page. If so, there may be ways around it.

Yes, a class must be defined before an instance of the class can exist. So you need to include the class definition before unserializing(sessions serialize/unserialize for you). A serialized object only stores the properties, not the methods.

If you use a serialized session to hold the object’s data, you shouldn’t need to instantiate it on PageB, all $_SESSION[‘PageA’] is now is an array.

Obviously calling the data will no longer be $App->PageA->Variable, it’d be unserialized and called by $_SESSION[‘PageA’][‘Variable’];

Unless you have to use the PageA object’s methods, you should be cool.

I hope I have that right if not someone please correct me!
I’ve never tried that before so I dont have an example because I haven’t found a good reason to attempt it thus far in my php journey :stuck_out_tongue:

I think its easier to instantiate it on pageB though, but maybe you have a good reason not to.

oh…didn’t know that methods do not get passed with the objects. well, let me explain what I’m trying to do.

I have this login class which instantiates a staff or member class based on the user logging in. this staff or member class will contain all the methods available to the user.

after creating the object, it redirects them to the respective page.

I could instantiate the class here instead, but I thought there should be some way to pass objects through pages with all the properties and methods intact?

You can still use methods on your object. I was just hinting at why php needs you to define the class before unserializing it.

$_SESSION['obj'] = $someObject;

require 'someClass.php';
$someObject = $_SESSION['obj'];

It’s probably not appropriate in this case — to keep the user object instantiated for their entire session would get very expensive very quickly in terms of memory, for little benefit.
Usual practice is to store the primary key in a session, then create the object again when it’s required using that.
If it ever is quicker/more beneficial to keep an object in memory, look into serialising and storing it in something like memcached.

i see. so I’m able to use the serialized object (with regards to properties and methods) just like it has been instantiated on pageB? (with the proper class included of course).

well because I’m working on multiple page rather than one index.php, I found that I keep on repeating codes.
So I thought if I could keep the object in memory, probably it’ll save some time coding and more importantly, saves on the number of modifications to go through should there be a change in future.

shall experiment with serialization. hmm…memcached? will have to do some reading up for that.

Why not use the singleton Registry pattern for passing objects?
I think its perfect in such cases. or not if i am wrong :slight_smile:

So, why not just check and assign a variable in session for the kind of staff and then on pageB use the class based on session var!