PHP Magic Method

Source: Internet
Author: User

<?php/**  Official Document:http://php.net/manual/zh/language.oop5.magic.php*/class magic {         public  $a  = array (1);     protected   $b  = array (2);    private  $c  = array (3);     public  $d  =  ';    public  ' $e  = array (' Huazi ' =>55) ;    /**     *  Construction Method       *   will be set when the class is in power this method      *  This method does not have to demonstrate the      */     function __construct ()     {         echo  ' construction Method: __construct () <br> ';    }             /**     *  when the object is serialized, this method is set first        *  If you do not have thisMagic method   will put   class properties   $a, $b, $c   Serialization      *  This method can specify that those properties are serialized       */    public function __sleep ()      {         return array (  ' A '  ,   ' B '  );     }            /**  The     *  object is deserialized when this method is first      */    public  function __wakeup ()      {         echo 1;    }        /**    The   *  method is used to respond to a class when it is treated as a string. For example  echo  $obj;  should show something.      *  This method must return a string, otherwise a fatal error of  E_RECOVERABLE_ERROR   level will be issued       */    public funCtion __tostring ()     {        return  ' echo  object? <br> ';     }            / **     *  from  PHP 5.1.0  when the  var_export ()    export class is called, this static The   method is called.       *  the only argument to this method is an array that contains press  array (' property '  => value, &NBSP, ...)   class properties arranged in the format.      */    public static function __set_state ($ Array)      {        return  $array [' d '];     }            /**      * PHP 5  introduces the concept of destructors, which is similar to other object-oriented language,     *  such as  c++. Destructors are removed when all references to an object are deleted or executed when an object is explicitly destroyed.       *      *  This method does not have to demonstrate the      */     Public function __destruct ()     {         //echo 55;    }             /**     *  when an inaccessible method is called in an object, __call ()   is called       */    public function __call ($name,  $arguments)  {         echo  ' __call ()  :  ';         //  Note:  $name   value is case-sensitive         var_dump ($name) ;         var_dump ($arguments);         echo  ' <br> ';         if (  $name  ==   ' abc '  )         {             $this $name ();        }    }     PRIVATE&NBSP;FUNCTION&NBSP;ABC ()     {        echo   ' 999<br> ';    }             /**     *  __callstatic ()   is called when a non-accessible method is called in a static manner.       */    public static function  __ callstatic  (  $name  ,   $arguments  )      {         echo  ' __callstatic ()  :  ';         //  Note:  $name   values are case-sensitive         var_dump ( $name);          var_dump ($arguments);        echo  ' <br> ';     }        /**     *   __set ()   is called when a value is assigned to an unreachable property.       */    public function __set (  $name, $val  )     {        echo  ' __set :  Method <br> ';         $this $name  =  $val;     }            /**      *  __get ()   is called when reading the value of an inaccessible property.        */    public function __get ( $ name )     {        echo  ' __get :   Methods <br> ';         return  $this $name;    }             /**     *  is called when a property is present       */    public function __isset ($name)      {         echo  ' __isset :  method <br> ';         return isset (  $this->e[$name] );     }             /**     *   when  unset ()    is called on an inaccessible property, __unset ()   is called.       */    public function __unset ($name)      {        echo  ' __unset :  method <br> ';         unset (  $this $name  );         var_dump ($name);    }             /**     *  called when the object is cloned       */    public function __clone ()     {         echo  ' __clone :  method ';         echo  ':  was cloned <br> ';    }             /**     *  When an attempt is made to invoke an object in a way that invokes a function, __ The Invoke ()   method is called automatically.       */    public function  __invoke  (   $x  )      {        echo  ' __ invoke :  method  <br> ';         var_dump  (  $x  );     } $ob  = new magic ();/** * __sleep ()   case  * var_dump (Serialize ($OB)); *//** * __wakeup  case  *  $ser  =  Serialize ($OB);  * unserialize ($ser); *//** * __tostring  case  * echo   $ob; *//** * __set_state  case   manual  * eval (' $b  =  '. Var_export ($ ob,true). '; ');  * var_dump ($b); *//** * __call  case  *  $ob->meiyou ();  * $ OB-&GT;ABC (; *//** * __callstatic ) Case   * magic::kao (a);  *//**  * __set  case  * print_r ($ob); *  $ob->c=2; * print_r ($ob);  * ** * __get  Case  * print_r ($ob->c); *//** * __isset  case  *  var_dump ( isset ($ob->huazi)  ); *//** * __unset  case  * unset ($ OB-&GT;B); * print_r ($OB); *//** * __clone  case  *  $c  = clone  $ob;  */ /** * __invoke  Case  *  $ob (' hehe ');  */class a{    function  __construct () {            }     function __destruct () {             }    function __call (  $name, $val  ) {             }    static function __callstatic ($ name,  $arguments)  {    }    function __get ($name) {             }    function  __set ($name, $val) {            }     function __unset ($name) {            }    function  __isset ($name) {            }     function __tostring ()  {    }    function __ Set_state ($array)  {    }    function __invoke () {             }    function __clone () {             }    function  __sleep () {    }    function __wakeup () {     }    }


PHP Magic Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.