3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo {} class Bar { public function __construct($param1) {} } $reflection = new \ReflectionClass('Foo'); var_dump($reflection->newInstanceArgs(array())); try { var_dump($reflection->newInstanceArgs(array(1))); } catch (Exception $e) { var_dump($e); } $reflectionB = new \ReflectionClass('Bar'); var_dump($reflectionB->newInstanceArgs(array())); var_dump($reflectionB->newInstanceArgs(array(1))); var_dump($reflectionB->newInstanceArgs(array(1,2)));
Output for 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, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
object(Foo)#2 (0) { } object(ReflectionException)#3 (7) { ["message":protected]=> string(83) "Class Foo does not have a constructor, so you cannot pass any constructor arguments" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/bVEgs" ["line":protected]=> int(11) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/bVEgs" ["line"]=> int(11) ["function"]=> string(15) "newInstanceArgs" ["class"]=> string(15) "ReflectionClass" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> array(1) { [0]=> int(1) } } } } ["previous":"Exception":private]=> NULL } Fatal error: Uncaught ArgumentCountError: Too few arguments to function Bar::__construct(), 0 passed and exactly 1 expected in /in/bVEgs:5 Stack trace: #0 [internal function]: Bar->__construct() #1 /in/bVEgs(18): ReflectionClass->newInstanceArgs(Array) #2 {main} thrown in /in/bVEgs on line 5
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20
object(Foo)#2 (0) { } object(ReflectionException)#3 (7) { ["message":protected]=> string(83) "Class Foo does not have a constructor, so you cannot pass any constructor arguments" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/bVEgs" ["line":protected]=> int(11) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(9) "/in/bVEgs" ["line"]=> int(11) ["function"]=> string(15) "newInstanceArgs" ["class"]=> string(15) "ReflectionClass" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> array(1) { [0]=> int(1) } } } } ["previous":"Exception":private]=> NULL } Warning: Missing argument 1 for Bar::__construct() in /in/bVEgs on line 5 object(Bar)#4 (0) { } object(Bar)#4 (0) { } object(Bar)#4 (0) { }

preferences:
197.48 ms | 404 KiB | 183 Q