Jump to content

PHP Serialization


c_pattle

Recommended Posts

I was looking for a way to use to same instance of a class over multiple pages (e.g with the same property values) and I came across serialization.  However the only articles I could find on it where quite a few years old.  Is this still a good practise?  Also if it is how to you recommend I do it?  I have experimented serializing objects to session variables, is this a good method or should I use text files or databases instead?

 

Thanks for any help. 

Link to comment
https://forums.phpfreaks.com/topic/232457-php-serialization/
Share on other sites

You can either create an instance of your class in a session variable or you can simply copy the instance to/from a session variable. The class definition needs to exist before the session_start() statement so that the object can be recreated.

 

It is not and never was necessary to serialize an object to store it in a session variable.

Link to comment
https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195717
Share on other sites

Thanks

 

I tried to copy my class instance into a session variable but I am having some problems.  On my index page I have this copied my class to a session variable and then on my other page I am doing this

 

$db = new dbClass();
$contentclass = new contentClass($db);

session_start();

$contentclass = $_SESSION['contentclass'];

 

When I create the class I am passing a database connection using dependency injection.  However when I copy my from session variable it effectively clears the connection meaning I can use any of the method in my class.  Does anyone know what I should do? 

 

Thanks for any help. 

 

 

Link to comment
https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195838
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.