The Magic Method in PHP

Source: Internet
Author: User
1. __construct () and __destruct ()

It is called when the instance is created/destroyed, and can pass 0 or more parameters.

  Class A  {    function A ()    {      echo "build a";    }    function __destruct ()    {      echo "destroy A";    }  }  $obj = new A ();  

Note:the destructor method would be called as soon as there is no other references to a particular object, or in any order During the shutdown sequence.

With regard to constructors, PHP5.3.3, a method defined in a class named in a particular namespace is no longer considered a constructor. The constructor is still the same as the original in the namespace-free class. Such as:

Namespace Foo;class Bar {public    function bar () {        //treated as constructor in PHP 5.3.0-5.3.2        //treated a s regular method as of PHP 5.3.3    }}

If there is no namespace Foo; Bar () will also be used as a constructor. In addition, if the following conditions exist:

  function __construct ()    {      echo "construct A";    }    function A ()    {      echo "build A";    }    function __destruct ()    {      echo "destroy A";    }  }

That is, contains both __construct () and a function with the same name as the class name, then only __construct () is called.

2. __call () and __callstatic ()

This method is called when an attempt is to invoke a nonexistent method. Two parameters, one is the method name, and the other is the parameter array of the called method.

Class methodtest{public    function __call ($name, $arguments)    {        //Note:value of $name are case sensitive.        echo "Calling object method ' $name '"             . Implode (", $arguments). "
"; } public static function __callstatic ($name, $arguments) { //Note:value of $name are case sensitive. echo "Calling static method ' $name '" . Implode (", $arguments). "
";

Where $arguments is passed in as an array. Operation Result:

Calling object method ' Runtest ' in Object contextcalling static method ' Runtest ' in static context

Also note the scope of the function protected and private:

Class Testmagiccallmethod {public    function foo ()    {        echo __method__. Php_eol. "
"; } Public Function __call ($method, $args) { echo __method__. Php_eol. "
"; if (Method_exists ($this, $method)) { $this-$method (); } } protected function Bar () { echo __method__. Php_eol. "
"; } Private function Baz () { echo __method__. Php_eol. "
"; }} $test = new Testmagiccallmethod (); $test->foo ();/** * Outputs: * Testmagiccallmethod::foo */$test Bar ();/** * Outputs: * Testmagiccallmethod::__call * testmagiccallmethod::bar * * $test->baz ();/** * Outputs: * Testmagi Ccallmethod::__call * Testmagiccallmethod::baz * *

3.__get () and __set ()

Called when attempting to read a property that does not exist for an object.

Note: We can use this function to implement various actions like reflection in Java.

Class test{public   function __get ($key)   {      echo $key. "Not exists";   }   Public Function __set ($key, $value)   {      echo $key. "=". $value;   }} $t = new Test (); Echo $t->name. "
"; $t->name =" abc "; output: Name not existsname = ABC

4. __tostring ()

This method is similar to the ToString () method of Java, and when we print the object directly, the callback uses this function, and the function must return a string.

Class test{    Private $name = "abc";    Private $age = n;    Public Function __tostring ()    {        return ' name: $this->name, Age: $this->age ";    }} $t = new Test (); Echo $t; output: Name:abc, Age:12

There are also __isset (), __unset (), __sleep (), __wakeup (), __tostring (), __invoke (), __set_state () and __clone () without repeating them.

  • Related Article

    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.