3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Allows value objects to expose their properties while maintaining immutability. * * Define your properties in the docblock for your class with {@code @property}. */ class Value { private $a; public function __construct($a) { $this->a = $a; } public function __get($aProperty) { if (property_exists($this, $aProperty)) { return $this->{$aProperty}; } $this->triggerError("Undefined property: %s::$%s", $aProperty); } public function __set($aProperty, $aValue) { if (property_exists($this, $aProperty)) { $this->triggerError("Cannot access private property %s::$%s", $aProperty); } $this->triggerError("Undefined property: %s::$%s", $aProperty); } private function triggerError($message, $aProperty) { $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); throw new ErrorException(sprintf($message, __CLASS__, $aProperty), 0, E_USER_NOTICE, $backtrace[2]['file'], $backtrace[2]['line']); } } $v = new Value(1); var_dump($v->a); var_dump($v->b); $v->a = 3; $v->b = 4;

preferences:
30.56 ms | 407 KiB | 5 Q