The Magic method in PHP Class (Magic methods) Concise summary _php instance

Source: Internet
Author: User

1. __construct () and __destruct ()

Invoked when an instance is created/destroyed, you can pass 0 or more parameters.

Class A
 {
  function A ()
  {
   echo "build A";
  }

  function __destruct ()
  {
   echo "destroy A";
  }
 }

 $obj = new A ();
 Unset ($obj);

Note:the destructor method would be called as soon as there are no other references to a particular object During the shutdown sequence.

With respect to constructors, PHP5.3.3 begins, a method that is defined as a class name in a particular namespace is no longer considered a constructor. In a class that is not namespace, it is still a constructor. Such as:

namespace Foo;
Class Bar {public
  function bar () {
    //treated as constructor in PHP 5.3.0-5.3.2
    //treated as regular meth Od as of PHP 5.3.3
  }
}

If there is no namespace Foo; Then bar () will also be treated 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 contains both __construct () and a function with the same name as the class name, only the __construct () is invoked.

2. __call () and __callstatic ()

This method is called when an attempt is to invoke a method that does not exist. Two parameters, one is the method name, and one is the parameter array of the invoked method.

Class Methodtest
{public
  function __call ($name, $arguments)
  {
    //Note:value of $name was case sensit Ive.
    echo "Calling object method ' $name '"
       . Implode (", $arguments). "<br>";
  }

  public static function __callstatic ($name, $arguments)
  {
    //Note:value of $name was case sensitive.
    echo "Calling static method ' $name '"
       . Implode (", $arguments). "<br>";
  }

$obj = new Methodtest;
$obj->runtest (' in ', ' object ', ' context ');
Methodtest::runtest (' In ', ' static ', ' context ');

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

Calling object method ' Runtest ' in the object context
Calling static method ' Runtest ' in the static context

Also note the scope of the function protected and private:

Class Testmagiccallmethod {public
  function foo ()
  {
    echo __method__. Php_eol. " <br> ";
  }

  Public Function __call ($method, $args)
  {
    echo __method__. Php_eol. " <br> ";
    if (Method_exists ($this, $method))
    {
      $this-> $method ();
    }
  }
  
  protected function Bar ()
  {
    echo __method__. Php_eol. " <br> ";
  }

  Private function Baz ()
  {
    echo __method__. Php_eol. " <br> ";
  }
}

$test  =  new Testmagiccallmethod ();
$test->foo ();
/**
 * Outputs:
 * testmagiccallmethod::foo

/$test->bar ();
/**
 * Outputs:
 * testmagiccallmethod::__call
 * Testmagiccallmethod::bar
 /

$test->baz ();
/**
 * Outputs:
 * testmagiccallmethod::__call
 * Testmagiccallmethod::baz
 *

3.__get () and __set ()

Called when an attempt is made to read a property that does not exist for an object.

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

Class Test
{public
  function __get ($key)
  {
   echo $key. "Not exists";
  }
  The Public Function __set ($key, $value)
  {
   echo $key. "=". $value;
  }
}

$t = new Test ();
echo $t->name. " <br> ";
$t->name = "abc";

Output:
Name NOT EXISTS
name = ABC

4. __tostring ()

This method is similar to the Java ToString () method, 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 =;

  Public Function __tostring ()
  {return
    ' name: $this->name, Age: $this->age ';
  }
}

$t = new Test ();
Echo $t;

Output:

NAME:ABC, Age:12

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.