MagicMethod 1. _ construct () and _ destruct () in PHP ()
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 contextCalling 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 existsname = 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
There are also _ isset (), _ unset (), _ sleep (), _ wakeup (), _ toString (), _ invoke (), _ set_state () and _ clone () are not described in detail.