class Person { - private $name;
- private $age;
- private $id;
function __construct( $name, $age ) { - $this->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:
class Account { - public $balance;
- function __construct( $balance ) {
- $this->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 too
- print $person2->account->balance;
// output: - // 210
- ?>
官方文档:http://cn2.php.com/__clone |