日期:2014-05-17 浏览次数:20501 次
$object2 = clone $object;
<?php
class Person {
private $name;
private $age;
function __construct($name, $age) {
$this->name=$name;
$this->age=$age;
}
function say() {
echo "我的名字叫:".$this->name."<br />";
echo "我的年龄是:".$this->age;
}
}
$p1 = new Person("张三", 20);
$p2 = clone $p1;
$p2->say();
?>
我的名字叫:张三 我的年龄是:20
<?php
class Person {
private $name;
private $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
function say() {
echo "我的名字叫:".$this->name;
echo " 我的年龄是:".$this->age."<br />";
}
function __clone() {
$this->name = "我是假的".$this->name;
$this->age = 30;
}
}
$p1 = new Person("张三", 20);
$p1->say();
$p2 = clone $p1;
$p2->say();
?>
我的名字叫:张三 我的年龄是:20 我的名字叫:我是假的张三 我的年龄是:30