This article describes MagicMethod in PHP, including _ construct (), _ destruct (), and _ call (), _ callStatic (), _ get (), _ set (), _ toString (), etc. For more information, see 1. _ construct () and _ destruct ()
If an instance is called when it is created or destroyed, zero or multiple parameters can be passed.
class A { function A() { echo "build A"; } function __destruct() { echo "destroy A"; } } $obj = new A(); //unset($obj);
Note: The destructor method will be called as soon as there are no other references to a participant object, or in any order during the shutdown sequence.
With regards to Constructor, a class defined in a particular namespace named after the class name will no longer be considered as a constructor at the beginning of PHP5.3.3. In a namespace-free class, it is still a constructor. For example:
namespace Foo;class Bar { public function Bar() { // treated as constructor in PHP 5.3.0-5.3.2 // treated as regular method as of PHP 5.3.3 }}
If namespace Foo; is not available, Bar () is also 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 is, if _ construct () and a function with the same name as the class name are included, only _ construct () is called ().
2. _ call () and _ callStatic ()
This method is called when you try to call a method that does not exist. 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 is case sensitive. echo "Calling object method '$name' " . implode(' ', $arguments). "
"; } public static function __callStatic($name, $arguments) { // Note: value of $name is case sensitive. echo "Calling static method '$name' " . implode(' ', $arguments). "
"; }} $obj = new MethodTest;$obj->runTest('in','object','context');MethodTest::runTest('in','static','context');
$ Arguments is passed as an array. Running result:
Calling object method 'runtest' in object context
Calling static method 'runtest' in static context
Also note the function scopes 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: * TestMagicCallMethod::__call * TestMagicCallMethod::baz */
3. _ get () and _ set ()
It is called when you try to read an attribute that does not exist in an object.
Note: We can use this function to perform operations similar to 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 exists
Name = abc
4. _ toString ()
This method is similar to the toString () method of java. when we print the object directly, we call this function back. The function must return a string.
class Test{ private $name = "abc"; private $age = 12; public function __toString() { return "name : $this->name, age : $this->age"; }} $t = new Test();echo $t;
Output:
Name: abc, age: 12
For more brief summary of Magic methods in the PHP class, please follow the PHP Chinese network!