name = $name; $this->age = $age; } function setid( $id ) {
$this->id = $id; } function __clone() { $this->id = 0; }}print ;
$person = new person( bob, 44 );$person->setid( 343 );$person2 = clone $person;print_r( $person );print_r( $person2 );print ;?>
复制代码
演示代码2:
balance = $balance; }}class person {
private $name; private $age; private $id; public $account; function __construct( $name, $age, account $account ) {
$this->name = $name; $this->age = $age; $this->account = $account; } function setid( $id ) {
$this->id = $id; } function __clone() {
$this->id = 0; }}$person = new person( bob, 44, new account( 200 ) );
$person->setid( 343 );$person2 = clone $person;// give $person some money
$person->account->balance += 10;// $person2 sees the credit tooprint $person2->account->balance;// output:
// 210?>
复制代码
官方文档:http://cn2.php.net/__clone