Home >Backend Development >PHP Tutorial >PHP method _set_state()

PHP method _set_state()

不言
不言Original
2018-04-10 10:22:512753browse

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(&#39;property&#39;=>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[&#39;var1&#39;];        $obj->var2 = $an_array[&#39;var2&#39;];        return $obj;
    }
}$a = new A;$a->var1 = 5;$a->var2 = &#39;foo&#39;;eval(&#39;$b = &#39; . var_export($a, true) . &#39;;&#39;); // $b = A::__set_state(array(
                                            //    &#39;var1&#39; => 5,
                                            //    &#39;var2&#39; => &#39;foo&#39;,
                                            // ));var_dump($b);?>

         

For more information, please visit the PHP gallery

<?php//  PHP魔术方法之__set_state()//该方法的唯一参数是一个数组,其中包含array(&#39;property&#39;=>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[&#39;var1&#39;];        $obj->var2 = $an_array[&#39;var2&#39;];        return $obj;
    }
}$a = new A;$a->var1 = 5;$a->var2 = &#39;foo&#39;;eval(&#39;$b = &#39; . var_export($a, true) . &#39;;&#39;); // $b = A::__set_state(array(
                                            //    &#39;var1&#39; => 5,
                                            //    &#39;var2&#39; => &#39;foo&#39;,
                                            // ));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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn