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) { $obj = clone $this; foreach ($obj as $index => $name) { var_dump([$index, $name, $values]); 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/rEvVe on line 7 Deprecated: Creation of dynamic property Struct::$y is deprecated in /in/rEvVe on line 7 Deprecated: Creation of dynamic property Struct::$z is deprecated in /in/rEvVe on line 7 array(3) { [0]=> string(1) "x" [1]=> NULL [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } array(3) { [0]=> string(1) "y" [1]=> NULL [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } array(3) { [0]=> string(1) "z" [1]=> int(0) [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } Struct Object ( [x] => [y] => [z] => 0 ) array(3) { [0]=> string(1) "x" [1]=> NULL [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } array(3) { [0]=> string(1) "y" [1]=> NULL [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } array(3) { [0]=> string(1) "z" [1]=> int(0) [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } Struct Object ( [x] => [y] => [z] => 0 ) Deprecated: Creation of dynamic property Struct::$firstname is deprecated in /in/rEvVe on line 7 Deprecated: Creation of dynamic property Struct::$lastname is deprecated in /in/rEvVe on line 7 array(3) { [0]=> string(9) "firstname" [1]=> NULL [2]=> array(2) { [0]=> string(7) "William" [1]=> string(7) "Wallace" } } array(3) { [0]=> string(8) "lastname" [1]=> NULL [2]=> array(2) { [0]=> string(7) "William" [1]=> string(7) "Wallace" } } Struct Object ( [firstname] => [lastname] => ) array(3) { [0]=> string(9) "firstname" [1]=> NULL [2]=> array(2) { [0]=> string(5) "Peter" [1]=> string(7) "Griffin" } } array(3) { [0]=> string(8) "lastname" [1]=> NULL [2]=> array(2) { [0]=> string(5) "Peter" [1]=> string(7) "Griffin" } } Struct Object ( [firstname] => [lastname] => )
Output for 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
array(3) { [0]=> string(1) "x" [1]=> NULL [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } array(3) { [0]=> string(1) "y" [1]=> NULL [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } array(3) { [0]=> string(1) "z" [1]=> int(0) [2]=> array(3) { [0]=> int(12) [1]=> int(45) [2]=> int(-34) } } Struct Object ( [x] => [y] => [z] => 0 ) array(3) { [0]=> string(1) "x" [1]=> NULL [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } array(3) { [0]=> string(1) "y" [1]=> NULL [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } array(3) { [0]=> string(1) "z" [1]=> int(0) [2]=> array(2) { [0]=> int(100) [1]=> int(-100) } } Struct Object ( [x] => [y] => [z] => 0 ) array(3) { [0]=> string(9) "firstname" [1]=> NULL [2]=> array(2) { [0]=> string(7) "William" [1]=> string(7) "Wallace" } } array(3) { [0]=> string(8) "lastname" [1]=> NULL [2]=> array(2) { [0]=> string(7) "William" [1]=> string(7) "Wallace" } } Struct Object ( [firstname] => [lastname] => ) array(3) { [0]=> string(9) "firstname" [1]=> NULL [2]=> array(2) { [0]=> string(5) "Peter" [1]=> string(7) "Griffin" } } array(3) { [0]=> string(8) "lastname" [1]=> NULL [2]=> array(2) { [0]=> string(5) "Peter" [1]=> string(7) "Griffin" } } Struct Object ( [firstname] => [lastname] => )
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/rEvVe on line 11
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '.', expecting '&' or T_VARIABLE in /in/rEvVe 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/rEvVe on line 11
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '.', expecting ')' in /in/rEvVe on line 11
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 '.', expecting ')' in /in/rEvVe on line 11
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/rEvVe on line 11
Process exited with code 255.

preferences:
249.28 ms | 401 KiB | 356 Q