1--TEST-- 2ReflectionClass::newInstance() 3--CREDITS-- 4Robin Fernandes <robinf@php.net> 5Steve Seear <stevseea@php.net> 6--FILE-- 7<?php 8 9class B { 10 public function __construct($a, $b) { 11 echo "In constructor of class B with args $a, $b\n"; 12 } 13} 14 15class C { 16 protected function __construct() { 17 echo "In constructor of class C\n"; 18 } 19} 20 21class D { 22 private function __construct() { 23 echo "In constructor of class D\n"; 24 } 25} 26 27class E { 28} 29 30$rcB = new ReflectionClass('B'); 31$rcC = new ReflectionClass('C'); 32$rcD = new ReflectionClass('D'); 33$rcE = new ReflectionClass('E'); 34 35try { 36 var_dump($rcB->newInstance()); 37} catch (Throwable $e) { 38 echo "Exception: " . $e->getMessage() . "\n"; 39} 40try { 41 var_dump($rcB->newInstance('x', 123)); 42} catch (Throwable $e) { 43 echo "Exception: " . $e->getMessage() . "\n"; 44} 45 46try { 47 $rcC->newInstance(); 48 echo "you should not see this\n"; 49} catch (Exception $e) { 50 echo $e->getMessage() . "\n"; 51} 52 53try { 54 $rcD->newInstance(); 55 echo "you should not see this\n"; 56} catch (Exception $e) { 57 echo $e->getMessage() . "\n"; 58} 59 60$e1 = $rcE->newInstance(); 61var_dump($e1); 62 63try { 64 $e2 = $rcE->newInstance('x'); 65 echo "you should not see this\n"; 66} catch (Exception $e) { 67 echo $e->getMessage() . "\n"; 68} 69?> 70--EXPECTF-- 71Exception: Too few arguments to function B::__construct(), 0 passed and exactly 2 expected 72In constructor of class B with args x, 123 73object(B)#%d (0) { 74} 75Access to non-public constructor of class C 76Access to non-public constructor of class D 77object(E)#%d (0) { 78} 79Class E does not have a constructor, so you cannot pass any constructor arguments 80