Scenario for Serializing an Object?
Moderator: General Moderators
-
thinsoldier
- Forum Contributor
- Posts: 367
- Joined: Fri Jul 20, 2007 11:29 am
- Contact:
Scenario for Serializing an Object?
Could someone describe for me a scenario where it makes sense to serialize an object and save it in $_SESSION? I can't think of one.
Warning: I have no idea what I'm talking about.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Scenario for Serializing an Object?
Scenario #1: You have an ecommerce site and your shopping cart is an object. For the cart to be available on all pages you need to save it somewhere. Many would use a session, however the PHP session handler serializes/unserializes this for you (though in a slightly different format than serialize()).thinsoldier wrote:Could someone describe for me a scenario where it makes sense to serialize an object and save it in $_SESSION? I can't think of one.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Re: Scenario for Serializing an Object?
SESSION stores state. Objects bind state to behavior. When you have state bound to behavior, and the state must persist page one page load. If there is no behavior than an array makes more sense (SESSION is already an array).
Take AbraCadaver's example. You don't want to loop thru an array building up items, manually checking their quantities on every page (duplication). Nor would you even want to have to loop thru the session creating an 'item' object for the cart. (even though its less duplication, its still duplication). You could pull a regular PHP array out of the object, and save that... but thats still something you'd have to remember to do on every page load, whereas the object just gets added to the session and then is always there. The object can "decide" to change its state while we remain ignorant of it doing so.
Take AbraCadaver's example. You don't want to loop thru an array building up items, manually checking their quantities on every page (duplication). Nor would you even want to have to loop thru the session creating an 'item' object for the cart. (even though its less duplication, its still duplication). You could pull a regular PHP array out of the object, and save that... but thats still something you'd have to remember to do on every page load, whereas the object just gets added to the session and then is always there. The object can "decide" to change its state while we remain ignorant of it doing so.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Scenario for Serializing an Object?
Yes the main reason, like the shopping cart example, is where you need a set of objects one multiple pages -- and the objects would require some non-trivial initialization if re-created on every page.
(#10850)