|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-04-13 14:09 UTC] tthiery at yahoo dot de
I define classes in extra scripts like this one:
File: hello.class.php
<?
class Hello {
function print() {
echo "Hello";
}
}
?>
I include and create them via a manufacturing function:
$obj = myCreateObject("Hello");
myCreateObject lookup the file for Hello, include_once them, create the object and returns it.
But then the problem: I store the object to session and when I unserialize the object in the next script, an error occurs. Clear, the class script file isn't loaded. Until now I do the serialize/unserialize via a trick (a register_shutdown_function store all of these objects AND their meta data like classname in one session var and in the next script all of them are unserialized manually).
My Question: Is in the unserialize function and/or the session_decode function a possibility of a callback function like myLoadClass("Hello") for a serialized object (class Hello) ?
And if the answer is a no: Hey PHP team it's an idea for a new function.
ps I post this message already in the zend.com forum PHP into future
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 18:00:01 2025 UTC |
__autoload() feature of PHP5 can be used for that. I tried the following code and it works. __autoload() is called when the class name is unkown. In this case the user is responsible for loading the class. p.php <?php function __autoload($cname) { var_dump('class name'.$cname); require_once $cname.'.class.php'; } function getInstance($cname) { require_once $cname.'.class.php'; return new $cname(); } if (!is_file('ser.dat')) { $a = getInstance("hello"); file_put_contents('ser.dat', serialize($a)); } else { $a = unserialize(file_get_contents('ser.dat')); } var_dump($a); ?> hello.class.php : <?php class hello { public $hello_var ="123"; function hello() { printf("hello contstructor\n"); } } ?>