3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php trait Immutable { private $_defaultValues = []; private $_userDefinedValues = []; private $_userDefinedProperties = []; private static $_doNotTakeOverProperties = [ '_defaultValues' => true, '_userDefinedValues' => true, '_userDefinedProperties' => true, ]; final public function __construct() { // 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; } $foo = new Foo; $foo->poo = 1; $foo->poo = 2;
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught LogicException: You can not overwrite the value for property "poo" in /in/bIJqi:32 Stack trace: #0 /in/bIJqi(56): Foo->__set('poo', 2) #1 {main} thrown in /in/bIJqi on line 32
Process exited with code 255.
Output for 5.4.11 - 5.6.28
Fatal error: Uncaught exception 'LogicException' with message 'You can not overwrite the value for property "poo"' in /in/bIJqi:32 Stack trace: #0 /in/bIJqi(56): Foo->__set('poo', 2) #1 {main} thrown in /in/bIJqi on line 32
Process exited with code 255.
Output for 5.3.21 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/bIJqi on line 3
Process exited with code 255.