Created
October 6, 2012 22:04
-
-
Save mpmont/3846318 to your computer and use it in GitHub Desktop.
__set_state() method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
{ | |
public $var1; | |
public $var2; | |
public static function __set_state($an_array) // As of PHP 5.1.0 | |
{ | |
$obj = new A; | |
$obj->var1 = $an_array['var1']; | |
$obj->var2 = $an_array['var2']; | |
return $obj; | |
} | |
} | |
$a = new A; | |
$a->var1 = 5; | |
$a->var2 = 'foo'; | |
eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array( | |
// 'var1' => 5, | |
// 'var2' => 'foo', | |
// )); | |
var_dump($b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment