fork download
  1. <?php
  2.  
  3. class my_class
  4. {
  5. public $var1;
  6.  
  7. public $var2;
  8.  
  9. public $var3;
  10.  
  11. private $data;
  12.  
  13. public function __construct()
  14. {
  15. $this->data = new stdClass;
  16. $this->data->var1 = 'a';
  17. $this->data->var2 = 'b';
  18. $this->data->var3 = 'c';
  19. $this->assignReferences();
  20. }
  21.  
  22. public function __clone()
  23. {
  24. $this->data = json_decode(json_encode($this->data));
  25. $this->assignReferences();
  26. }
  27.  
  28. private function assignReferences()
  29. {
  30. $this->var1 = &$this->data->var1;
  31. $this->var2 = &$this->data->var2;
  32. $this->var3 = &$this->data->var3;
  33. }
  34. }
  35.  
  36. $original = new my_class;
  37. $new = clone $original;
  38. $new->var3 = 'd';
  39.  
  40. echo $original->var3; // Output Should Be "c"
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
c