За последние 24 часа нас посетил 53931 программист и 1721 робот. Сейчас ищут 834 программиста ...

function __clone

Тема в разделе "Прочие вопросы по PHP", создана пользователем T-Mon, 18 апр 2008.

  1. T-Mon

    T-Mon Активный пользователь

    С нами с:
    2 янв 2008
    Сообщения:
    67
    Симпатии:
    0
    Адрес:
    Kyiv
    Я прикрепил файл с кодом - почему (если запустить код на выполение) id второго объекта при копировании отображается правильно, но потом почему-то становится, как у первого:
    P.S. Не нашел, как тут крепяться файлы, выкладываю так:
    PHP:
    1. <?php
    2. define("DEBUG",TRUE);
    3. main();
    4.  
    5. class myVar
    6. {
    7.     private static $_globAmount;
    8.     private static $_globRealAmount;
    9.     private $_varVal;
    10.     private $_varId;
    11.  
    12.     public function __construct($value = NULL)
    13.     {
    14.         self::$_globAmount++;
    15.         self::$_globRealAmount++;
    16.         $this->_varId = self::$_globAmount;
    17.         $this->_varVal = $value;
    18.         if (DEBUG)
    19.         {
    20.             $report = "An object has been constructed. (Type \"".__CLASS__."\", id \"".$this->_varId."\", value \"".$this->_varVal."\") \n";
    21.             echo $report;
    22.         }
    23.         return;
    24.     }
    25.  
    26.     public function __destruct()
    27.     {
    28.         if (DEBUG)
    29.         {
    30.             $report = "An object has been destructed. (Type \"".__CLASS__."\", id \"".$this->_varId."\", value \"".$this->_varVal."\") \n";
    31.             echo $report;
    32.         }
    33.         self::$_globRealAmount--;
    34.         $this->_varId = NULL;
    35.         $this->_varVal = NULL;
    36.         return;
    37.     }
    38.  
    39.     public function __clone()
    40.     {
    41.         self::$_globAmount++;
    42.         self::$_globRealAmount++;
    43.         $that->_varId = self::$_globAmount;
    44.         $that->_varVal = $this->_varVal;
    45.         if (DEBUG)
    46.         {
    47.             $report = "An object has been copied. (Type \"".__CLASS__."\", id \"".$this->_varId."\", value \"".$this->_varVal."\") \n";
    48.             echo $report;
    49.             $report = "An object has been constructed. (Type \"".__CLASS__."\", id \"".$that->_varId."\", value \"".$that->_varVal."\") \n";
    50.             echo $report;
    51.         }
    52.         return;
    53.     }
    54.  
    55.     protected function __toString()
    56.     {
    57.         $varStr = "An object. (Type \"".__CLASS__."\", id \"".$this->_varId."\", value \"".$this->_varVal."\") \n";
    58.         return $varStr;
    59.     }
    60.  
    61.     final protected function __set($varName,$varVal)
    62.     {
    63.         $errorMsg = "Attribute ".$varName." is private or doesn't exist! So ".gettype($varVal)." value (".$varVal.") will be lost!";
    64.         die($errorMsg);
    65.         return;
    66.     }
    67.  
    68.     final protected function __get($varName)
    69.     {
    70.         $errorMsg = "Attribute ".$varName." is private or doesn't exist!";
    71.         die($errorMsg);
    72.         return;
    73.     }
    74.  
    75.     final protected function __call($funcName)
    76.     {
    77.         $errorMsg = "Method ".$funcName." is private or doesn't exist!";
    78.         die($errorMsg);
    79.         return;
    80.     }
    81.  
    82.     final public static function getRealAmount()
    83.     {
    84.         if (DEBUG)
    85.         {
    86.             $report = "Number of existing variables - ".self::$_globRealAmount.". \n";
    87.             echo $report;
    88.         }
    89.         return self::$_globRealAmount;
    90.     }
    91.  
    92.     final public static function getAmount()
    93.     {
    94.         if (DEBUG)
    95.         {
    96.             $report = "Number of created variables - ".self::$_globAmount.". \n";
    97.             echo $report;
    98.         }
    99.         return self::$_globAmount;
    100.     }
    101.  
    102.     public function getId()
    103.     {
    104.         if (DEBUG)
    105.         {
    106.             $report = "The object id is: ".$this->_varId.". \n";
    107.             echo $report;
    108.         }
    109.         return $this->_varId;
    110.     }
    111.  
    112.     public function getValue()
    113.     {
    114.         if (DEBUG)
    115.         {
    116.             $report = "The object value is: ".$this->_varVal.". \n";
    117.             echo $report;
    118.         }
    119.         return $this->_varVal;
    120.     }
    121.  
    122.     public function setValue($value)
    123.     {
    124.         $this->_varVal = $value;
    125.         if (DEBUG)
    126.         {
    127.             $report = "An object has been modified. (Type \"".__CLASS__."\", id \"".$this->_varId."\", value \"".$this->_varVal."\") \n";
    128.             echo $report;
    129.         }
    130.         return $this->_varVal;
    131.     }
    132. }
    133.  
    134. function main()
    135. {
    136.     $a = new myVar(12);
    137.     myVar::getRealAmount();
    138.     myVar::getAmount();
    139.     $b = clone $a;
    140.     $b->getId();
    141.     $b->setValue(123);
    142.     myVar::getRealAmount();
    143.     myVar::getAmount();
    144.     echo $a;
    145.     echo $b;
    146.     $a = NULL;
    147.     $b = NULL;
    148.     myVar::getRealAmount();
    149.     myVar::getAmount();
    150. }
    151.  
    152. ?>