3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Human { private $name, $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $data = [["John", 23], ["Nick", 32], ["Ann", 17]]; $humans = []; foreach($data as $item) { $r = new ReflectionClass("Human"); $humans[] = $r->newInstance($item); } var_dump($humans);
Output for 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, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Human::__construct(), 1 passed and exactly 2 expected in /in/COnUL:6 Stack trace: #0 [internal function]: Human->__construct(Array) #1 /in/COnUL(19): ReflectionClass->newInstance(Array) #2 {main} thrown in /in/COnUL on line 6
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.0.0 - 7.0.20
Warning: Missing argument 2 for Human::__construct() in /in/COnUL on line 6 Notice: Undefined variable: age in /in/COnUL on line 9 Warning: Missing argument 2 for Human::__construct() in /in/COnUL on line 6 Notice: Undefined variable: age in /in/COnUL on line 9 Warning: Missing argument 2 for Human::__construct() in /in/COnUL on line 6 Notice: Undefined variable: age in /in/COnUL on line 9 array(3) { [0]=> object(Human)#2 (2) { ["name":"Human":private]=> array(2) { [0]=> string(4) "John" [1]=> int(23) } ["age":"Human":private]=> NULL } [1]=> object(Human)#1 (2) { ["name":"Human":private]=> array(2) { [0]=> string(4) "Nick" [1]=> int(32) } ["age":"Human":private]=> NULL } [2]=> object(Human)#3 (2) { ["name":"Human":private]=> array(2) { [0]=> string(3) "Ann" [1]=> int(17) } ["age":"Human":private]=> NULL } }

preferences:
222.38 ms | 404 KiB | 259 Q