Advertisement
Guest User

__get overloading in class inheritance

a guest
Oct 1st, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2. class myActiveRecord
  3. {
  4.     protected $party = 'Wanna party?';
  5.    
  6.     public function __get($name) {
  7.         echo 'myActiveRecord __get() called' . '<br />';
  8.         $method = 'get' . ucfirst($name);
  9.         if(method_exists($this, $method)) {
  10.             $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  11.             foreach($backtrace  as $b) {
  12.                 if($b['function'] == $method) {
  13.                
  14.                     return parent::__get($name);
  15.                 }
  16.             }
  17.  
  18.             return $this->$method();
  19.         }
  20.  
  21.         return parent::__get($name);
  22.     }
  23. }
  24.  
  25. class test extends myActiveRecord
  26. {
  27.    
  28.     public function getParty()
  29.     {
  30.         echo 'test getParty() called' . '<br />';
  31.         return $this->party;
  32.     }
  33.    
  34.    
  35.     //overrides myActiveRecords' __get()
  36.     public function __get($name)
  37.         {
  38.             echo 'test __get() called' . '<br />';
  39.             if ($name == 'party') return $this->$name;
  40.             else return parent::__get($name);
  41.         }
  42. }
  43.  
  44. $test = new test();
  45. echo '<br />Accessing through ->party :<br />';
  46. echo $test->party;
  47. echo '<br />';
  48. echo '<br />Accessing through getParty() :<br />';
  49. echo $test->getParty();
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement