-
-
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 "
";</li>
<li>$person = new Person( "bob", 44 );</li>
<li>$person->setId( 343 );</li>
<li>$person2 = clone $person;</li>
<li>print_r( $person );</li>
<li>print_r( $person2 );</li>
<li>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.net/__clone
|