3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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)));
based on fIkCS
Output for 7.1.0 - 7.2.0
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 - 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) { }
Output for 5.3.20 - 5.4.45
object(Foo)#2 (0) { } object(ReflectionException)#2 (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) { }