var_export($obj,true) 导出一个合法的php代码,返回一个字符串
eval($str) 执行一个字符串代码
__set_state 当用var_export导出一个类时,自动调用,必须static
<?php class Person { private $name; private $age; public function __construct($name,$age) { $this->name = $name; $this->age = $age; } static function __set_state($arr) { $p = new Person($arr[‘name‘],$arr[‘age‘]); $p->name = "ls"; $p->age = 20; return $p; } function __toString() { return $this->name."{$this->age}"; } } $a = new Person("zs",10); echo $a; echo ‘<br>‘; $str = var_export($a,true); eval(‘$b=‘.$str.‘;‘); echo $b;
时间: 2024-10-23 02:14:41