After PHP5.0, PHP has more object-oriented methods, making PHP more powerful! !
Some functions called magic methods in PHP are introduced here: In fact, in general applications, we all need to use them! !
1.__construct() When instantiating an object, this method of the object is first called.
Java code
class Test { function __construct() { echo "before"; } } $t = new Test();
class Test { function __construct() { echo "before"; } } $t = new Test();
The output is:
start
We know that the php5 object model and the function with the same class name are the constructors of the class, so if we define the constructor and __construct() method at the same time, php5 will call the constructor by default and not the __construct() function, so_ _construct() as the default constructor of the class
2.__destruct() This method is called when an object is deleted or the object operation terminates.
Java code
class Test { function __destruct() { echo "end"; } } $t = new Test(); will output end
class Test { function __destruct() { echo "end"; } } $t = new Test(); will output end
We can perform operations such as releasing resources when the object operation is completed
3.__get() is called when trying to read a property that does not exist.
If you try to read a property of an object that does not exist, PHP will give an error message. If we add the __get method to the class, we can use this function to implement various operations similar to reflection in Java.
Java code
class Test { public function __get($key) { echo $key . " does not exist"; } } $t = new Test(); echo $t->name; will output: name does not exist
class Test { public function __get($key) { echo $key . " does not exist"; } } $t = new Test(); echo $t->name; will output: name does not exist
4.__set() is called when trying to write a value to a property that does not exist.
Java code
class Test { public function __set($key,$value) { echo 'pair'.$key . "value".$value; } } $t = new Test(); $t->name = "aninggo" ; will output: adding value to name aninggo
class Test { public function __set($key,$value) { echo 'pair'.$key . "value".$value; } } $t = new Test(); $t->name = "aninggo" ; will output: adding value to name aninggo
5.__call() This method is called when trying to call a method that does not exist on the object.
Java code
class Test { public function __call($Key, $Args) { echo "The {$Key} method you want to call does not exist. The parameters you passed in are: " . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go);
class Test { public function __call($Key, $Args) { echo "The {$Key} method you want to call does not exist. The parameters you passed in are: " . print_r($Args, true); } } $t = new Test(); $t->getName(aning,go);
The program will output:
Java code
The getName method you are trying to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)
The getName method you are trying to call does not exist.The parameters are: Array
(
[0] => aning
[1] => go
)
6.__toString() is called when printing an object
This method is similar to java's toString method. When we print the object directly, we call this function
class Test { public function __toString() { return "Print Test"; } } $t = new Test(); echo $t;
When echo $t; is run, $t->__toString(); will be called to output
Print Test
7.__clone() is called when the object is cloned
class Test { public function __clone() { echo "I have been cloned!"; } }$t = new Test(); $t1 = clone $t;Program output: I have been cloned!