为什么出错?
这是phpe.net上的例子可我运行时出错
<?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(); //这是32句
//输出: 1 Zeev's Object
print($ot->getId() . " " . $ot->getName() . "<br>");
//输出: 2 Clone of Zeev's Object
print($ot2->getId() . " " . $ot2->getName() . "<br>");
?>
Fatal error: Cannot call __clone() method on objects - use 'clone $obj' instead in E:\myweb\tm-shop\class\1.php on line 32
问题点数:20、回复次数:4Top
1 楼yuelengxin(键步随风)回复于 2004-10-03 21:38:45 得分 0
类,不懂啊!
我测试了,在第4行就出错了,晕....
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in E:\htdocs\example\源码练习\源码测试\untitled.php on line 4
关注ing!Top
2 楼lisoon(http://www.lisoon.com/FlashRss/)回复于 2004-10-03 22:10:04 得分 0
5.0下测试没有问题。
请检查配置。Top
3 楼xuzuning(唠叨)回复于 2004-10-04 10:50:48 得分 20
$ot2 = $ot->__clone(); //这是32句
===>
$ot2 = clone $ot;
这是php5正式版的一个变化,你的代码是运行于老的开发版的
另外第16行$this->name = "Clone of $that->name"; 中的$that应改为$this
Top
4 楼tmyu()回复于 2004-10-04 13:47:04 得分 0
唠叨哥,我试过$ot2 = clone $ot;他只显示Clone of
看来是网上介绍的老了,他上面是用that
谢谢唠叨哥,行了Top




