PHP magic _ PHP Tutorial

Source: Internet
Author: User
PHP magic. PHP magic method 1, _ get _ set: Take Over object properties 2, _ call _ callStatic: control the use of php object methods 3, _ toString: convert a php object to a string 4, _ invoke: PHP magic method

1. _ get/_ set: take over the object property
2. _ call/_ callStatic: controls the use of php object methods.
3. _ toString: converts a php object to a string.
4. _ invoke: calls back this method when a php object is executed as a function.

class Object{ protected $array = array();    function __set($key,$value)    {        var_dump(__METHOD__);        $this->array[$key] = $value;    }    function __get($key)    {        var_dump(__METHOD__);        return $this->array[$key];    }    function __call($func,$param)    {        var_dump($func,$param);        return magic mathod __call;    }    static function __callStatic($func,$param)    {        var_dump($func,$param);        return magic mathod __callStatic;    }    function __toString()    {        return __toString;    }    function __invoke($param)    {        var_dump($param);        return invoke;    }}

$ Obj = new Object ();

$ Obj-> title = "Hello"; // when a value is assigned to an attribute that does not exist in an object, the _ set method is automatically called.

Echo $ Obj-> title; // when you read an attribute that does not exist in an object, it automatically calls the _ get method.

Echo $ Obj-> test ("hello", "123"); // when a method that does not exist in an object is called, The _ call method is automatically called.

Echo $ Obj: test1 ("hello1", "1234"); // when a static method does not exist in an object is called, The _ callStatic method is automatically called.

Echo $ Obj; // when an object is directly output (because the object cannot be output directly), The _ toString method is automatically called to convert the object to a string.

Echo $ Obj ("hello"); // when an object is used as a function, the _ invoke method is automatically called.

Lifecycle 1, _ get/_ set: Take Over object properties 2, _ call/_ callStatic: control the use of php object methods 3, _ toString: convert the php object to string 4, _ invoke :...

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.