php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #16591 class loading in unserialize/session_decode
Submitted: 2002-04-13 14:09 UTC Modified: 2004-01-17 10:40 UTC
From: tthiery at yahoo dot de Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.1.2 OS: Linux/Win2k/whatever
Private report: No CVE-ID: None
 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-17 10:40 UTC] andrey@php.net
__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");
        }
}
?>

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 23 16:00:03 2025 UTC