11/06/2012

__PHP_Incomplete_Class Object vs __PHP_Incomplete_Class_Name

When you meet '__PHP_Incomplete_Class_Name' by using unserialize , please confirm whether you have included the class file in serialized contents before you use unserialize function.

For example:

Include class
/*SimpleClass.php*/
class SimpleClass{
    var $key;
    function SimpleClass(){
     
    }
    function __set($name,$value){
        $this->key[$name] = $value;
    }
    function __get($name){
        return $this->key[$key];
    }
}
?>
Write file

/*Store.php
 */
require_once './SimpleClass.php';
$SimpleClass = new SimpleClass;
$SimpleClass->keywords = 'Google';
@file_put_contents('./aaa.txt', serialize($SimpleClass));
?>


Wrong Read file 

/*Read.php
 */
$SimpleClass = unserialize(file_get_contents('./aaa.txt'));
print_r($SimpleClass);
?>

Wrong Output 

__PHP_Incomplete_Class Object
(
    [__PHP_Incomplete_Class_Name] => SimpleClass
    [key] => Array
        (
            [keywords] => Google
        )
)
In order to resolve this problem, you have to include the class file in serialized contents before you use unserialize function.

Correct Read File


/*Read.php
 */
require_once './SimpleClass.php';
$SimpleClass = unserialize(file_get_contents('./aaa.txt'));
print_r($SimpleClass);
?>

Correct Output

SimpleClass Object
(
    [key] => Array
        (
            [keywords] => Google
        )
)




1 comment: