3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class test { protected $_a = 6; function __get($key) { echo "__get({$key})\n"; if($key == 'stuff') { return $this->stuff(); } else if($key == 'a') { return $this->_a + 2; } } function stuff() { echo "stuff()\n"; return array('random' => 'key', 'using_getter' => 10 * $this->a); } function hey() { return $this->a; } } class me extends test { function __get( $name){ $val = parent::__get($name); if ($name === 'a' ) { $val = parent::a - 5; } return $val; } } $test = new test(); print 'this should be 60: '.$test->stuff['using_getter'].PHP_EOL; print 'this should be 6: '.$test->a.PHP_EOL; print $test->hey(); $me = new me(); print $me->a;
based on JgLYD
Output for 7.0.0 - 7.2.0
__get(stuff) stuff() __get(a) this should be 60: 80 __get(a) this should be 6: 8 __get(a) 8__get(a) Fatal error: Uncaught Error: Undefined class constant 'a' in /in/uf1Mq:32 Stack trace: #0 /in/uf1Mq(47): me->__get('a') #1 {main} thrown in /in/uf1Mq on line 32
Process exited with code 255.
Output for 5.3.20 - 5.6.28
__get(stuff) stuff() __get(a) this should be 60: 80 __get(a) this should be 6: 8 __get(a) 8__get(a) Fatal error: Undefined class constant 'a' in /in/uf1Mq on line 32
Process exited with code 255.