PHP/Class/clone — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 07:00, 26 мая 2010

Change class instance after clone

<?php
   class Employee {
   
      private $employeeid;
      private $tiecolor;
      function setEmployeeID($employeeid) {
         $this->employeeid = $employeeid;
      }
      function getEmployeeID() {
         return $this->employeeid;
      }
      function setTiecolor($tiecolor) {
         $this->tiecolor = $tiecolor;
      }
      function getTiecolor() {
         return $this->tiecolor;
      }
   }
   $drone1 = new Employee();
   $drone1->setEmployeeID("2222");
   $drone1->setTiecolor("red");
   $drone2 = clone $drone1;
   $drone2->setEmployeeID("1111");
   echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />";
   echo "drone1 tie color: ".$drone1->getTiecolor()."<br />";
   echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />";
   echo "drone2 tie color: ".$drone2->getTiecolor()."<br />";
?>



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;
?>



clone class instance

<?php
   class Employee {
   
      private $employeeid;
      private $tiecolor;
      function setEmployeeID($employeeid) {
         $this->employeeid = $employeeid;
      }
      function getEmployeeID() {
         return $this->employeeid;
      }
      function setTiecolor($tiecolor) {
         $this->tiecolor = $tiecolor;
      }
      function getTiecolor() {
         return $this->tiecolor;
      }
      function __clone() {
         $this->tiecolor = "blue";
      }
   }
   $drone1 = new Employee();
   $drone1->setEmployeeID("1111");
   
   $drone2 = clone $drone1;
   $drone2->setEmployeeID("1111");
   echo "drone1 employeeID: ".$drone1->getEmployeeID()."<br />";
   echo "drone2 employeeID: ".$drone2->getEmployeeID()."<br />";
   echo "drone2 tiecolor: ".$drone2->getTiecolor()."<br />";
?>



Cloning an aggregated class

 
<?
class Address { 
  protected $city;
  protected $country;
 
  public function setCity($city) { $this->city = $city; }
  public function getCity() { return $this->city; } 
  public function setCountry($country) { $this->country = $country; }
  public function getCountry() { return $this-> country;}
}
class Person {
 protected $name;
 protected $address;
 public function __construct() { $this->address = new Address; }
 public function setName($name) { $this->name = $name; }
 public function getName() { return $this->name; }
 public function __call($method, $arguments) { 
  if (method_exists($this->address, $method)) { 
   return call_user_func_array( array($this->address, $method), $arguments);
  }
 }
}
$a = new Person;
$a->setName("A");
$a->setCity("B");
$z = clone $a;
$z->setName("Z");
$z->setCity("T");
print $a->getName() . " lives in " . $a->getCity() . ".";
print $z->getName() . " lives in " . $z->getCity() . ".";
?>



Copying Objects

 
<?
    class DogTag {
            public $Words;
    }
    class Dog {
            public $Name;
            public $DogTag;
            public function bark( ) {
                    print "Woof!\n";
            }
            public function _ _construct($DogName) {
                    print "Dog: $DogName\n";
                    $this->Name = $DogName;
                    $this->DogTag = new DogTag;
                    $this->DogTag->Words = "$DogName:555-1234";
            }
    }
    class Poodle extends Dog {
            public function bark( ) {
                    print "Yip!\n";
            }
    }
    $poppy = new Poodle("Poppy");
    print $poppy->DogTag->Words . "\n";
    
    function namechange($dog) {
            $dog->Name = "Dozer";
    }
    namechange($poppy);
    print $poppy->Name . "\n";
    namechange(clone $poppy);
?>



Parents clone

 
<?
    abstract class Dog {
            public function __clone( ) {
                    echo "In dog clone\n";
            }
    }
    class Poodle extends Dog {
            public $Name;
                    public function __clone( ) {
                    echo "In poodle clone\n";
                    parent::__clone( );
            }
    }
    $poppy = new Poodle( );
    $poppy->Name = "Poppy";
    $rover = clone $poppy;
?>



Properly implementing cloning in aggregated classes

 
class Person {
 public function __clone() { 
  $this->address = clone $this->address;
 }
}