Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.