3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Immutable { private $_defaultValues = []; private $_userDefinedValues = []; private $_userDefinedProperties = []; private static $_doNotTakeOverProperties = [ '_defaultValues' => true, '_userDefinedValues' => true, '_userDefinedProperties' => true, ]; final public function __construct() { var_dump("in trait constructor"); // take over all user-defined non-static properties foreach ((new \ReflectionObject($this))->getProperties() as $property) { $propertyName = $property->getName(); if (isset(self::$_doNotTakeOverProperties[$propertyName]) || $property->isStatic()) { continue; } $this->_userDefinedProperties[$propertyName] = true; $this->_defaultValues[$propertyName] = $property->getValue($this); unset($this->{$property->getName()}); } } final public function __set($name, $value) { if (!isset($this->_userDefinedProperties[$name])) { throw new \LogicException('Unknown property "' . $name . '"'); } if (array_key_exists($name, $this->_userDefinedValues)) { throw new \LogicException('You can not overwrite the value for property "' . $name . '"'); } $this->_userDefinedValues[$name] = $value; } final public function __get($name) { if (!isset($this->_userDefinedProperties[$name])) { throw new \LogicException('Unknown property "' . $name . '"'); } if (array_key_exists($name, $this->_userDefinedValues)) { return $this->_userDefinedValues[$name]; } return $this->_defaultValues[$name]; } } class Foo { use Immutable; public $poo; public function __construct() { var_dump("in class constructor"); parent::__construct(); } } $foo = new Foo; $foo->poo = 1; $foo->poo = 2;
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.4, 8.3.6
Fatal error: Cannot use "parent" when current class scope has no parent in /in/o5Aeq on line 57
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Cannot use "parent" when current class scope has no parent in /in/o5Aeq on line 57
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: Cannot use "parent" when current class scope has no parent in /in/o5Aeq on line 57 string(20) "in class constructor" Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parent in /in/o5Aeq:57 Stack trace: #0 /in/o5Aeq(61): Foo->__construct() #1 {main} thrown in /in/o5Aeq on line 57
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
string(20) "in class constructor" Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parent in /in/o5Aeq:57 Stack trace: #0 /in/o5Aeq(61): Foo->__construct() #1 {main} thrown in /in/o5Aeq on line 57
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
string(20) "in class constructor" Fatal error: Cannot access parent:: when current class scope has no parent in /in/o5Aeq on line 57
Process exited with code 255.

preferences:
161.76 ms | 402 KiB | 198 Q