PHPÖÐ __set_state()°ì·¨Ïê½â
×÷Óãº
×Ô PHP 5.1.0 Æ𣬵±µ÷È¡ var_export() Êä³öÀàʱ£¬´Ë¾²Ì¬°ì·¨»á±»Ö÷¶¯µ÷È¡¡£
²ÎÊý£º
±¾°ì·¨µÄ¶ÀÒ»²ÎÊýÊÇÒ»¸öÊý×飬ÆäÖаüÀ¨°´ array('property' => value, ...) ¸ñ¾Ö°ÚÁеÄÀàÊôÐÔ¡£
ÏÂÃæÎÒÃÇÏÈÀ´¿´¿´ÔÚûÓÐ¼Ó __set_state() ×´¿ö°´Ï£¬´úÂë¼°ÔËת½á¹ûÔõÑù£º
ÉÏ´úÂ룺
<?php class Person { public $sex; public $name; public $age; public function __construct($name="", $age=25, $sex='ÄÐ') { $this->name = $name; $this->age = $age; $this->sex = $sex; } } $person = new Person('СÃ÷'); // ³õʼ¸³Öµ var_export($person);
¿´½á¹û£º
Person::__set_state(array( 'sex' => 'ÄÐ', 'name' => 'СÃ÷', 'age' => 25, ))
ºÜÃ÷ÏÔ£¬½«¶ÔÏóÖеÄÊôÐÔ¶¼´òÓ¡³öÀ´ÁË
¼ÓÁË __set_state() Ö®ºó£º
Á¬ÐøÉÏ´úÂ룺
<?php class Person { public $sex; public $name; public $age; public function __construct($name="", $age=25, $sex='ÄÐ') { $this->name = $name; $this->age = $age; $this->sex = $sex; } public static function __set_state($an_array) { $a = new Person(); $a->name = $an_array['name']; return $a; } } $person = new Person('СÃ÷'); // ³õʼ¸³Öµ $person->name = 'Сºì'; var_export($person);
Á¬Ðø¿´½á¹û£º
Person::__set_state(array( 'sex' => 'ÄÐ', 'name' => 'Сºì', 'age' => 25, ))
ÒÔÉϾÍÊÇPHPÖÐ __set_state()°ì·¨Ïê½âµÄ¾ßÌåÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢°Ù·Ö°ÙÔ´ÂëÍøÆäËüÏà¹ØÎÄÕ£¡
¸ÐлÄúµÄÖ§³Ö£¬ÎÒ»á¼ÌÐøŬÁ¦µÄ!
´ò¿ªÖ§¸¶±¦É¨Ò»É¨£¬¼´¿É½øÐÐɨÂë´òÉÍŶ
°Ù·Ö°ÙÔ´ÂëÍø ½¨Òé´òÉÍ1¡«10Ôª£¬ÍÁºÀËæÒ⣬¸ÐлÄúµÄÔĶÁ£¡