PHP/Class/ clone

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

Using the __clone() Method

   <source lang="html4strict">

<?php

    class myObject {
         public $var_one = 10;
         public $var_two = 20;
         function __clone() {
                   $this->var_two = 0;
         }
    }
    $inst_one = new myObject();
    $inst_two = clone $inst_one;
    var_dump($inst_one);
    var_dump($inst_two);

?>

 </source>
   
  


Using the clone Statement

   <source lang="html4strict">

<?php

    class Integer {
         private $number;
         public function getInt() {
              return (int)$this->number;
         }
         public function setInt($num) {
              $this->number = (int)$num;
         }
    }
    
    $class_one = new Integer();
    $class_one_copy = clone $class_one;

?>

 </source>