c_pattle Posted April 1, 2011 Share Posted April 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 1, 2011 Share Posted April 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195717 Share on other sites More sharing options...
betterphp Posted April 1, 2011 Share Posted April 1, 2011 It depends what you are doing really. I sometimes store a serialised array in a text file to cache some data (form an API for example) but for some things a database will make more sense. Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195718 Share on other sites More sharing options...
c_pattle Posted April 2, 2011 Author Share Posted April 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195838 Share on other sites More sharing options...
yozyk Posted April 2, 2011 Share Posted April 2, 2011 You cannot strore database connection in session variables. All database conections will be closed after your php script has been executed. Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1195847 Share on other sites More sharing options...
betterphp Posted April 8, 2011 Share Posted April 8, 2011 You will only be able to store data, not an active resource like a connection or a file pointer. I don’t see why you would want to store the connection like this :? Quote Link to comment https://forums.phpfreaks.com/topic/232457-php-serialization/#findComment-1198743 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.