PHP/Class/clone

Материал из Web эксперт
Версия от 10:00, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Change class instance after clone

   <source lang="html4strict">

<?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()."
"; echo "drone1 tie color: ".$drone1->getTiecolor()."
"; echo "drone2 employeeID: ".$drone2->getEmployeeID()."
"; echo "drone2 tie color: ".$drone2->getTiecolor()."
";

?>

      </source>
   
  


clone a class

   <source lang="html4strict">

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

      </source>
   
  


clone class instance

   <source lang="html4strict">

<?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()."
"; echo "drone2 employeeID: ".$drone2->getEmployeeID()."
"; echo "drone2 tiecolor: ".$drone2->getTiecolor()."
";

?>

      </source>
   
  


Cloning an aggregated class

   <source lang="html4strict">

<? 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() . "."; ?>

 </source>
   
  


Copying Objects

   <source lang="html4strict">

<?

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

?>

 </source>
   
  


Parents clone

   <source lang="html4strict">

<?

   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;

?>

 </source>
   
  


Properly implementing cloning in aggregated classes

   <source lang="html4strict">

class Person {

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

}

 </source>