PHP魔术方法__clone()篇

PHP中定义了一个内置方法__clone()来调整兑现的克隆行为;

当一个对象被克隆的时候会自动执行__clone()方法,而复制的对象可以在其方法内进行调整

 1 header('Content-type:text/html;charset="utf-8"');
 2 /*存在内置方法_clone()的类*/
 3 class Computer{
 4     public $_name;
 5     public function __clone(){
 6         $this->_name = "IBM";
 7     }
 8 }
 9 
10 /*没有__clone()会产生一个错误
11  *当对象克隆时会自动调用__clone()方法
12  * */
13 $computer1 = new Computer();
14 $computer1->_name = "dell";
15 echo $computer1->_name;     //输出值:dell
16 $computer2 = clone $computer1;
17 echo $computer2->_name;     //输出值:IBM
原文地址:https://www.cnblogs.com/changshaoshuai/p/3288359.html