Home >Backend Development >PHP Tutorial >PHP method _set_state()
The content introduced in this article is about the PHP method _set_state(). Now I share it with everyone. Friends in need can refer to it
For more information, please visit the PHP gallery
<?php// PHP魔术方法之__set_state()//该方法的唯一参数是一个数组,其中包含array('property'=>value,...)格式排列的属性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);?>
For more information, please visit the PHP gallery
<?php// PHP魔术方法之__set_state()//该方法的唯一参数是一个数组,其中包含array('property'=>value,...)格式排列的属性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);?>
Related recommendations:
Example comparison of two methods of php method overloading
The above is the detailed content of PHP method _set_state(). For more information, please follow other related articles on the PHP Chinese website!