Home > Article > Backend Development > Detailed explanation of PHP magic method __clone (code example)
1. Understand the definition of magic methods in PHP
2. Understand the usage scenarios of __clone() magic method
3. Master the usage of __clone() magic method
PHP reserves all class methods starting with __ (two underscores) as magic method. Therefore, when defining class methods, except for the above magic methods, it is recommended not to prefix them with __.
When we want to do some special logical processing when cloning an object, we can define it in this class A magic method, this magic method is __clone, and then write your own special business logic in it
Summary:
1. In PHP, __clone is defined in the class in the following format: public function __clone(). Note here that there must be 2 __, which must be __clone, and it must take 0 parameters, one more and one less. Neither will work
2. When an object is cloned, the system will automatically trigger the defined __clone method of the class where the object is located
Each summary is obtained through practice. Now We use practice to demonstrate summaries, which can promote understanding and make each summary clearer and more intuitive
Practice goals:
1 . In PHP, __clone is defined in the class in the following format: public function __clone(). Note here that there must be 2 __, which must be __clone, and must take 0 parameters. One more or one less will not work.
The specific code is as follows:
<?php class Animal{ public function eat(){ } public function sleep(){ } //魔术方法 public function __clone(){ echo "自动执行了Animal类中的__clone方法<br/>"; } } $ani = new Animal(); ?>
The running result is:
Blank
Indicates that no error is reported
Next , let’s try adding parameters to this __clone method and see how the running result is
<?php class Animal{ public function eat(){ } public function sleep(){ } //魔术方法 public function __clone($name){ echo "自动执行了Animal类中的__clone方法<br/>"; } } $ani = new Animal(); ?>
The running result is:
Fatal error: Method Animal::__clone() cannot accept any arguments in D:\E-class\class-code\classing\index.php on line 11
Judging from the error message, this method cannot carry any parameters, so this is important Remember
Case 2,
Practical goals:
1. When cloning an object, the system will automatically trigger the defined_ _clone method
The specific code is as follows:
<?php class Animal{ public function eat(){ } public function sleep(){ } //魔术方法 public function __clone(){ echo "自动执行了Animal类中的__clone方法<br/>"; } } $ani = new Animal(); $ani2 = clone $ani; ?>
We found that we did not call the __clone method manually, that is to say, it was not written as $ani->__clone(), but this method is still Executed, why? Because we wrote $ani2 = clone $ani; which is equivalent to manually copying a $ani object, and then defining this magic method in this class, so it is like magic and is suddenly executed automatically
It must be noted here that there are two underscores, not one, otherwise it will not be a magic method. Let’s do the test again.
The specific code is as follows:
<?php class Animal{ public function eat(){ } public function sleep(){ } //魔术方法 试着少一个_ public function _clone(){ echo "自动执行了Animal类中的__clone方法<br/>"; } } $ani = new Animal(); $ani2 = clone $ani; ?>
The running result is :
Blank
It means that the __clone method has not been automatically called, so be sure to remember that there are two underscores in __, more or less will not work
1. This article mainly learns the specific usage scenarios and specific implementation methods of __clone magic method in php
I hope this article can bring you Please help me, thank you! ! !
The above is the detailed content of Detailed explanation of PHP magic method __clone (code example). For more information, please follow other related articles on the PHP Chinese website!