note 94332 deleted from language.oop5.magic by danbrown

From: Date: Thu, 29 Oct 2009 15:11:09 +0000
Subject: note 94332 deleted from language.oop5.magic by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162250@lists.php.net to get a copy of this message
Note Submitter: rarioj at gmail dot com ---- NOTE: If an object Foo has __set_state() method, but if that object contains another object Bar with no __set_state() method implemented, the resulting PHP expression will not be eval()-able. This is an example (object Test that contains an instance of Exception). <?php class Test { public $one; public $two; public function __construct($one, $two) { $this->one = $one; $this->two = $two; } public static function __set_state(array $array) { return new self($array['one'], $array['two']); } } $test = new Test('one', new Exception('test')); $string = var_export($test, true); /* $string = Test::__set_state(array( 'one' => 'one', 'two' => Exception::__set_state(array( 'message' => 'test', 'string' => '', 'code' => 0, 'file' => 'E:\\xampp\\htdocs\\test.Q.php', 'line' => 35, 'trace' => array ( ), 'previous' => NULL, )), )) */ eval('$test2 = '.$string.';'); // Fatal error: Call to undefined method Exception::__set_state ?> So avoid using var_export() on a complex array/object that contains other objects. Instead, use serialize() and unserialize() functions. <?php $string = 'unserialize('.var_export(serialize($test), true).')'; eval('$test2 = '.$string.';'); var_dump($test == $test2); // bool(true) ?>

« previous php.notes (#162250) next »