Persistent objects in PHP5

software development


I have a project where on the initial page I initialize two objects, a dB obj and a fileHandler obj. The dB object is initialized and then then the fileHandler obj is initialized, with a mysqli object returned from one of the dB objects methods (getMysqli()) passed in as a parameter. What I would like to do is save these objects in a session for use on other pages. When I assigned these instances to a session var like so:

$_SESSION[‘fileHandler’] = $fileHandlerObj;

I was able to access get methods to read properties of the object on another page via the session variable, but I wasnt able to execute methods in fileHandler code that called upon the mysqli object methods, such as query for example. The error I received was:

Fatal error: Call to a member function getMysqli() on a non-object in …

So obviously the object I passed into the fileHandler at initialization didn’t get carried over into the session variable. I have tried serializing/unserializing but nothing has produced different results. I am using PHP5, but persistent objects is somewhat new to me so Im sure there is something stupid I am overlooking. Any help would be appreciated, thanks.



I don’t know that much… but this might help:

“Persistant database connections will not work [with PHP-CGI, only as an Apache module]. PHP’s mysql_pconnect() function will just open a new connection because it can’t find a persistant one.”,PHP&Databases/_PHP#What.27s_the_difference_between_PHP-CGI_and_PHP_as_an_Apache_module.3F

Also, search this page for “object” for issues with storing objects in sessions:

All my sites are down at DH right now, so I’ve got nothing better to do than try to answer questions here in the forum. :slight_smile: