转:http://www.phpe.net/articles/399.shtml
<?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();
这一行应当改为
$ot2 = clone$ot;
*
*/
$ot2 = clone$ot;
//输出: 1 Zeev's Object
print($ot->getId() . " " . $ot->getName() . "<br>");
//输出: 2 Clone of Zeev's Object
print($ot2->getId() . " " . $ot2->getName() . "<br>");
?>
<?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();
这一行应当改为
$ot2 = clone$ot;
*
*/
$ot2 = clone$ot;
//输出: 1 Zeev's Object
print($ot->getId() . " " . $ot->getName() . "<br>");
//输出: 2 Clone of Zeev's Object
print($ot2->getId() . " " . $ot2->getName() . "<br>");
?>
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1232/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2008-9-9 16:35
评论列表