Guest User

Untitled

a guest
Sep 13th, 2014
986
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class A
  3. {
  4.     public function printVars()
  5.     {
  6.         $r = ' // get_called_class() == '.get_called_class().
  7.             ' // __CLASS__ == '.__CLASS__.
  8.             ' // get_object_vars($this):';
  9.         foreach(get_object_vars($this) as $key => $val){
  10.             $r .= ' ->'.$key.' == "'.$val.'"';
  11.         }
  12.         return $r;
  13.     }
  14. }
  15. class B extends A
  16. {
  17.     public function printVars() // actually overloading afaik
  18.     {
  19.         $r = ' // get_called_class() == '.get_called_class().
  20.             ' // __CLASS__ == '.__CLASS__.
  21.             ' // get_object_vars($this):';
  22.         foreach(get_object_vars($this) as $key => $val){
  23.             $r .= ' ->'.$key.' == "'.$val.'"';
  24.         }
  25.         return $r;
  26.     }
  27. }
  28. class C extends B
  29. {
  30. }
  31. echo '<pre>';
  32.  
  33. $a = new A;
  34. $a->that = 'that for A';
  35.  
  36. $b = new B;
  37. $b->that = 'that for B';
  38.  
  39. $c = new C;
  40.  
  41. echo '
  42. $a = new A;
  43. $a->that = \'that for A\';
  44.  
  45. $b = new B;
  46. $b->that = \'that for B\';
  47.  
  48. $c = new C;
  49.  
  50. $a->printVars();'.$a->printVars().'
  51. $b->printVars();'.$b->printVars().'
  52. $c->printVars();'.$c->printVars().'
  53. ';
  54.  
  55. $c->that = 'that for C';
  56. echo '
  57. $c->that = \'that for C\';
  58.  
  59. $a->printVars();'.$a->printVars().'
  60. $b->printVars();'.$b->printVars().'
  61. $c->printVars();'.$c->printVars().'
  62. ';
  63.  
  64. echo '<hr>
  65. using __set()';
  66.  
  67. class A2
  68. {
  69.     // private $that;
  70.     // protected $that;
  71.     public function __set($name,$value)
  72.     {
  73.         $this->$name = $value;
  74.     }
  75.     public function printVars()
  76.     {
  77.         $r = ' // get_called_class() == '.get_called_class().
  78.             ' // __CLASS__ == '.__CLASS__.
  79.             ' // get_object_vars($this):';
  80.         foreach(get_object_vars($this) as $key => $val){
  81.             $r .= ' ->'.$key.' == "'.$val.'"';
  82.         }
  83.         return $r;
  84.     }
  85. }
  86. class B2 extends A2
  87. {
  88.     // private $that;
  89.     // protected $that;
  90.     public function __set($name,$value)
  91.     {
  92.         $this->$name = $value;
  93.     }
  94.     public function printVars() // actually overloading afaik
  95.     {
  96.         $r = ' // get_called_class() == '.get_called_class().
  97.             ' // __CLASS__ == '.__CLASS__.
  98.             ' // get_object_vars($this):';
  99.         foreach(get_object_vars($this) as $key => $val){
  100.             $r .= ' ->'.$key.' == "'.$val.'"';
  101.         }
  102.         return $r;
  103.     }
  104. }
  105. class C2 extends B2
  106. {
  107.     // private $that;
  108.     // protected $that;
  109.     public function __set($name,$value)
  110.     {
  111.         $this->$name = $value;
  112.     }
  113. }
  114. echo '<pre>';
  115.  
  116. $a = new A2;
  117. $a->that = 'that for A2';
  118.  
  119. $b = new B2;
  120. $b->that = 'that for B2';
  121.  
  122. $c = new C2;
  123.  
  124. echo '
  125. $a = new A2;
  126. $a->that = \'that for A2\';
  127.  
  128. $b = new B2;
  129. $b->that = \'that for B2\';
  130.  
  131. $c = new C2;
  132.  
  133. $a->printVars();'.$a->printVars().'
  134. $b->printVars();'.$b->printVars().'
  135. $c->printVars();'.$c->printVars().'
  136. ';
  137.  
  138. $c->that = 'that for C2';
  139. echo '
  140. $c->that = \'that for C2\';
  141.  
  142. $a->printVars();'.$a->printVars().'
  143. $b->printVars();'.$b->printVars().'
  144. $c->printVars();'.$c->printVars().'
  145. ';
  146. ?>
  147. output:
  148. $a = new A;
  149. $a->that = 'that for A';
  150.  
  151. $b = new B;
  152. $b->that = 'that for B';
  153.  
  154. $c = new C;
  155.  
  156. $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
  157. $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
  158. $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this):
  159.  
  160. $c->that = 'that for C';
  161.  
  162. $a->printVars(); // get_called_class() == A // __CLASS__ == A // get_object_vars($this): ->that == "that for A"
  163. $b->printVars(); // get_called_class() == B // __CLASS__ == B // get_object_vars($this): ->that == "that for B"
  164. $c->printVars(); // get_called_class() == C // __CLASS__ == B // get_object_vars($this): ->that == "that for C"
  165.  
  166. using __set()
  167. $a = new A2;
  168. $a->that = 'that for A2';
  169.  
  170. $b = new B2;
  171. $b->that = 'that for B2';
  172.  
  173. $c = new C2;
  174.  
  175. $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
  176. $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
  177. $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this):
  178.  
  179. $c->that = 'that for C2';
  180.  
  181. $a->printVars(); // get_called_class() == A2 // __CLASS__ == A2 // get_object_vars($this): ->that == "that for A2"
  182. $b->printVars(); // get_called_class() == B2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for B2"
  183. $c->printVars(); // get_called_class() == C2 // __CLASS__ == B2 // get_object_vars($this): ->that == "that for C2"
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×