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 :...