clone a class : clone « Class « PHP






clone a class


<?php

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("Joe", 35, new Account( 2000 ) );
$person->setId( 111 );
$person2 = clone $person;

$person->account->balance += 110;
print $person2->account->balance;
?>

           
       








Related examples in the same category

1.clone class instance
2.Change class instance after clone
3.Cloning an aggregated class
4.Copying Objects
5.Parents clone
6.Properly implementing cloning in aggregated classes