1--TEST-- 2ReflectionClass::newInstance[Args] 3--FILE-- 4<?php 5 6function test($class) 7{ 8 echo "====>$class\n"; 9 try 10 { 11 $ref = new ReflectionClass($class); 12 } 13 catch (ReflectionException $e) 14 { 15 var_dump($e->getMessage()); 16 return; // only here 17 } 18 19 echo "====>newInstance()\n"; 20 try 21 { 22 var_dump($ref->newInstance()); 23 } 24 catch (ReflectionException $e) 25 { 26 var_dump($e->getMessage()); 27 } 28 catch (Throwable $e) 29 { 30 echo "Exception: " . $e->getMessage() . "\n"; 31 } 32 33 echo "====>newInstance(25)\n"; 34 try 35 { 36 var_dump($ref->newInstance(25)); 37 } 38 catch (ReflectionException $e) 39 { 40 var_dump($e->getMessage()); 41 } 42 43 echo "====>newInstance(25, 42)\n"; 44 try 45 { 46 var_dump($ref->newInstance(25, 42)); 47 } 48 catch (ReflectionException $e) 49 { 50 var_dump($e->getMessage()); 51 } 52 53 echo "\n"; 54} 55 56function __autoload($class) 57{ 58 echo __FUNCTION__ . "($class)\n"; 59} 60 61test('Class_does_not_exist'); 62 63Class NoCtor 64{ 65} 66 67test('NoCtor'); 68 69Class WithCtor 70{ 71 function __construct() 72 { 73 echo __METHOD__ . "()\n"; 74 var_dump(func_get_args()); 75 } 76} 77 78test('WithCtor'); 79 80Class WithCtorWithArgs 81{ 82 function __construct($arg) 83 { 84 echo __METHOD__ . "($arg)\n"; 85 var_dump(func_get_args()); 86 } 87} 88 89test('WithCtorWithArgs'); 90 91?> 92===DONE=== 93<?php exit(0); ?> 94--EXPECTF-- 95====>Class_does_not_exist 96__autoload(Class_does_not_exist) 97string(41) "Class Class_does_not_exist does not exist" 98====>NoCtor 99====>newInstance() 100object(NoCtor)#%d (0) { 101} 102====>newInstance(25) 103string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments" 104====>newInstance(25, 42) 105string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments" 106 107====>WithCtor 108====>newInstance() 109WithCtor::__construct() 110array(0) { 111} 112object(WithCtor)#%d (0) { 113} 114====>newInstance(25) 115WithCtor::__construct() 116array(1) { 117 [0]=> 118 int(25) 119} 120object(WithCtor)#%d (0) { 121} 122====>newInstance(25, 42) 123WithCtor::__construct() 124array(2) { 125 [0]=> 126 int(25) 127 [1]=> 128 int(42) 129} 130object(WithCtor)#%d (0) { 131} 132 133====>WithCtorWithArgs 134====>newInstance() 135Exception: Too few arguments to function WithCtorWithArgs::__construct(), 0 passed and exactly 1 expected 136====>newInstance(25) 137WithCtorWithArgs::__construct(25) 138array(1) { 139 [0]=> 140 int(25) 141} 142object(WithCtorWithArgs)#%d (0) { 143} 144====>newInstance(25, 42) 145WithCtorWithArgs::__construct(25) 146array(2) { 147 [0]=> 148 int(25) 149 [1]=> 150 int(42) 151} 152object(WithCtorWithArgs)#%d (0) { 153} 154 155===DONE=== 156