3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Struct { function __construct() { foreach (func_get_args() as $name) { $this->$name = null; } } function __invoke(... $values) { $obj = clone $this; foreach ($obj as $index => $name) { $obj->$index = array_shift($values); } return $obj; } } $Coord = new Struct('x', 'y', 'z'); $Coord->z = 0; $c1 = $Coord(12, 45, -34); print_r($c1); $c2 = $Coord(100, -100); print_r($c2); $Person = new Struct('firstname', 'lastname'); $p1 = $Person('William', 'Wallace'); print_r($p1); $p2 = $Person('Peter', 'Griffin'); print_r($p2);
based on jTZA7
Output for 5.6.8 - 5.6.28, 7.1.0
Struct Object ( [x] => 12 [y] => 45 [z] => -34 ) Struct Object ( [x] => 100 [y] => -100 [z] => ) Struct Object ( [firstname] => William [lastname] => Wallace ) Struct Object ( [firstname] => Peter [lastname] => Griffin )
Output for 7.0.0 - 7.0.14
Struct Object ( [x] => 45 [y] => -34 [z] => ) Struct Object ( [x] => -100 [y] => [z] => ) Struct Object ( [firstname] => Wallace [lastname] => ) Struct Object ( [firstname] => Griffin [lastname] => )
Output for 5.4.0 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/Fi8fQ on line 11
Process exited with code 255.
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected '.', expecting '&' or T_VARIABLE in /in/Fi8fQ on line 11
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '.', expecting '&' or T_VARIABLE in /in/Fi8fQ on line 11
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '.', expecting ')' in /in/Fi8fQ on line 11
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected '.', expecting ')' in /in/Fi8fQ on line 11
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/Fi8fQ on line 11
Process exited with code 255.