Home >Backend Development >PHP Tutorial >php magic method_PHP tutorial
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, then if we define both the constructor and __construct () method, php5 will call the constructor by default instead of calling the __construct() function, so __construct() serves as the default constructor of the class
2.__destruct() when deleting an object or object This method is called when the 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 then perform operations such as releasing resources at the end of the object operation
3.__get() When trying Called when reading a property that does not exist.
If you try to read a property that does not exist in an object, 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 Exists
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 .
"Additional value".$value; } } $t = new Test(); $t->name = "aninggo"; will output: pair name
Value-added aninggo
class Test { public function __set($key,$value) { echo
'right'.$key . "value".$value; } } $t = new Test(); $t->name = "aninggo";
It will output: append 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 "{$Key} you want to call
The method does not exist. The parameters you pass 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 want to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)
The getName method you want to call does not exist. The parameters are: Array
(
[0] => aning
[1] => go
)
6.__toString() is used when printing an object Calling
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 was copied!"; } }$t = new Test(); $t1 = clone $t;Program output: I have been cloned!
The original text is reproduced from Script House's "php magic method instructions"