长沙北大青鸟作者:科泰校区Leon Atkinson 翻译:Haohappy
<?php class ObjectTracker //对象跟踪器 { private static $nextSerial = 0; private $id; private $name; function __construct($name) //构造函数 { $this->name = $name; $this->id = ++self::$nextSerial; } function __clone() //克隆 { $this->name = "Clone of $that->name"; $this->id = ++self::$nextSerial; } function getId() //获取id属性的值 { return($this->id); } function getName() //获取name属性的值 { return($this->name); } } $ot = new ObjectTracker("Zeev's Object"); $ot2 = $ot->__clone(); //输出: 1 Zeev's Object print($ot->getId() . " " . $ot->getName() . "<br>"); //输出: 2 Clone of Zeev's Object print($ot2->getId() . " " . $ot2->getName() . "<br>"); ?>
注:本文章为原创文章,版权归文章作者与超越PHP网站所有,未经本站同意,禁止任何商业转载。非盈利网站及个人网站转载请注明出处,谢谢合作!