3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Creation of dynamic property Struct::$x is deprecated in /in/iVGGM on line 7 Deprecated: Creation of dynamic property Struct::$y is deprecated in /in/iVGGM on line 7 Deprecated: Creation of dynamic property Struct::$z is deprecated in /in/iVGGM on line 7 Struct Object ( [x] => 12 [y] => 45 [z] => -34 ) Struct Object ( [x] => 100 [y] => -100 [z] => 0 ) Deprecated: Creation of dynamic property Struct::$firstname is deprecated in /in/iVGGM on line 7 Deprecated: Creation of dynamic property Struct::$lastname is deprecated in /in/iVGGM on line 7 Struct Object ( [firstname] => William [lastname] => Wallace ) Struct Object ( [firstname] => Peter [lastname] => Griffin )
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Struct Object ( [x] => 12 [y] => 45 [z] => -34 ) Struct Object ( [x] => 100 [y] => -100 [z] => 0 ) Struct Object ( [firstname] => William [lastname] => Wallace ) Struct Object ( [firstname] => Peter [lastname] => Griffin )
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Fatal error: Function name must be a string in /in/iVGGM on line 29
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_VARIABLE in /in/iVGGM on line 14
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_VARIABLE in /in/iVGGM on line 14
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/iVGGM on line 14
Process exited with code 255.

preferences:
223.09 ms | 401 KiB | 346 Q