第五節--克隆 <?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>"); ?> |
溫馨提示:喜歡本站的話,請收藏一下本站!