Advertisement
h-collector

Untitled

Dec 4th, 2011
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. <?php
  2. class B{
  3.     public $a=0;
  4.     public $b=0;
  5. }
  6. class A extends ArrayObject{
  7.     public $genes;
  8.     public $pairs;
  9.     function __construct(){
  10.         $this->pairs = array(new B, new B, new B);
  11.         foreach($this->pairs as &$pair){
  12.             $this->genes[] = &$pair->a;
  13.             $this->genes[] = &$pair->b;
  14.         }
  15.     }
  16.     function __clone() {
  17.         $this->genes = array();
  18.         foreach ($this->pairs as $i => $pair){
  19.             $p = clone $pair;
  20.             $this->pairs[$i] = $p;
  21.             $this->genes[($i  )*2] = &$p->a;
  22.             $this->genes[($i+1)*2] = &$p->b;            
  23.         }
  24.     }
  25. }
  26.  
  27. $a = new A;
  28. $a->pairs[1]->a = 11;
  29. $a->genes[3] = 22;
  30. echo "print_r(\$a)\n";
  31. print_r($a);
  32.  
  33. $b = clone $a;
  34. echo "print_r(\$b)\n";
  35. print_r($b);
  36. $b->pairs[1]->a = 88;
  37. $b->genes[3] = 44;
  38. echo "print_r(\$b)\n";
  39. print_r($b);
  40. echo "print_r(\$a)\n";
  41. print_r($a);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement