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 = func_get_args(); $obj = clone $this; foreach (array_keys(get_object_vars($this)) as $index => $name) { if (isset($values[$index])) { $obj->$name = $values[$index]; } } 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); // If only: struct person { function __construct($firstname, $lastname) { $this->firstname = $firstname; $this->lastname = $lastname; } } // So we could: $p1 = person('William', 'Wallace'); // Instead of ubiquitous: $p1 = new Person(array('William', 'Wallace')); // or even $p1 = new Person(array('firstname' => 'William', 'lastname' => 'Wallace')); // or $p1 = Person::create(array('William', 'Wallace')); // etc
based on iVGGM
Output for 5.4.0 - 5.4.32
Parse error: syntax error, unexpected 'person' (T_STRING) in /in/5objM on line 47
Process exited with code 255.
Output for 5.1.0 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/5objM on line 47
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/5objM on line 47
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/5objM on line 14
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_VARIABLE in /in/5objM on line 14
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/5objM on line 14
Process exited with code 255.