3v4l.org

run code in 300+ PHP versions simultaneously
<?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;
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
__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 constant test::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 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
__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.5.24 - 5.5.35, 5.6.8 - 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.

preferences:
150.93 ms | 402 KiB | 183 Q