PHP method _set_state()

不言
Release: 2023-03-23 13:24:01
Original
2702 people have browsed it

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);?>
Copy after login
Copy after login

         

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);?>
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!