3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace Dshafik; class Struct { // Because gettype() and ReflectionType->__toString() are inconsistent /** * @var array Type mappings */ private $__typeMap = [ 'int' => 'integer', 'bool' => 'boolean', 'float' => 'double', ]; /** * @var array Reflection data */ private $__reflection = []; /** * @var array The struct values */ protected $__values = []; public function __construct() { $this->introspect(); $this->hoist(func_get_args()); } public function __get($what) { return $this->__values[$what]; } public function __set($what, $value) { if (($type = gettype($value)) === $this->__reflection[$what]) { return $this->__values[$what] = $value; } else { throw new \TypeError("Key '$what' must be of type {$this->__reflection[$what]}, $type given."); } } final protected function introspect() { if ($this->__reflection) { return; } $class = new \ReflectionObject($this); $constructor = $class->getConstructor(); foreach ($constructor->getParameters() as $arg) { $type = (string) $arg->getType(); $this->__reflection[$arg->name] = $this->__typeMap[$type] ?? $type; } } final protected function hoist($args) { $this->introspect(); $i = 0; foreach ($this->__reflection as $key => $type) { $this->__set($key, $args[$i]); $i++; } } } // With strict types, will throw a TypeError, or coerce otherwise on mismatch $struct = new class(1, "Hello") extends Struct { public function __construct(int $foo, string $bar) { $this->hoist(func_get_args()); } }; var_dump($struct); // Works $struct->foo = 2; var_dump($struct); // Will throw a TypeError $struct->foo = 'World';
Output for git.master, git.master_jit, rfc.property-hooks
object(Dshafik\Struct@anonymous)#1 (3) { ["__typeMap":"Dshafik\Struct":private]=> array(3) { ["int"]=> string(7) "integer" ["bool"]=> string(7) "boolean" ["float"]=> string(6) "double" } ["__reflection":"Dshafik\Struct":private]=> array(2) { ["foo"]=> string(7) "integer" ["bar"]=> string(6) "string" } ["__values":protected]=> array(2) { ["foo"]=> int(1) ["bar"]=> string(5) "Hello" } } object(Dshafik\Struct@anonymous)#1 (3) { ["__typeMap":"Dshafik\Struct":private]=> array(3) { ["int"]=> string(7) "integer" ["bool"]=> string(7) "boolean" ["float"]=> string(6) "double" } ["__reflection":"Dshafik\Struct":private]=> array(2) { ["foo"]=> string(7) "integer" ["bar"]=> string(6) "string" } ["__values":protected]=> array(2) { ["foo"]=> int(2) ["bar"]=> string(5) "Hello" } } Fatal error: Uncaught TypeError: Key 'foo' must be of type integer, string given. in /in/dC8Lu:42 Stack trace: #0 /in/dC8Lu(86): Dshafik\Struct->__set('foo', 'World') #1 {main} thrown in /in/dC8Lu on line 42
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
20.27 ms | 403 KiB | 8 Q