1--TEST-- 2ReflectionClass::newInstance() 3--CREDITS-- 4Robin Fernandes <robinf@php.net> 5Steve Seear <stevseea@php.net> 6--FILE-- 7<?php 8class A { 9 public function A() { 10 echo "In constructor of class A\n"; 11 } 12} 13 14class B { 15 public function __construct($a, $b) { 16 echo "In constructor of class B with args $a, $b\n"; 17 } 18} 19 20class C { 21 protected function __construct() { 22 echo "In constructor of class C\n"; 23 } 24} 25 26class D { 27 private function __construct() { 28 echo "In constructor of class D\n"; 29 } 30} 31class E { 32} 33 34 35$rcA = new ReflectionClass('A'); 36$rcB = new ReflectionClass('B'); 37$rcC = new ReflectionClass('C'); 38$rcD = new ReflectionClass('D'); 39$rcE = new ReflectionClass('E'); 40 41$a1 = $rcA->newInstance(); 42$a2 = $rcA->newInstance('x'); 43var_dump($a1, $a2); 44 45$b1 = $rcB->newInstance(); 46$b2 = $rcB->newInstance('x', 123); 47var_dump($b1, $b2); 48 49try { 50 $rcC->newInstance(); 51 echo "you should not see this\n"; 52} catch (Exception $e) { 53 echo $e->getMessage() . "\n"; 54} 55 56try { 57 $rcD->newInstance(); 58 echo "you should not see this\n"; 59} catch (Exception $e) { 60 echo $e->getMessage() . "\n"; 61} 62 63$e1 = $rcE->newInstance(); 64var_dump($e1); 65 66try { 67 $e2 = $rcE->newInstance('x'); 68 echo "you should not see this\n"; 69} catch (Exception $e) { 70 echo $e->getMessage() . "\n"; 71} 72?> 73--EXPECTF-- 74In constructor of class A 75In constructor of class A 76object(A)#%d (0) { 77} 78object(A)#%d (0) { 79} 80 81Warning: Missing argument 1 for B::__construct() in %s on line 9 82 83Warning: Missing argument 2 for B::__construct() in %s on line 9 84 85Notice: Undefined variable: a in %s on line 10 86 87Notice: Undefined variable: b in %s on line 10 88In constructor of class B with args , 89In constructor of class B with args x, 123 90object(B)#%d (0) { 91} 92object(B)#%d (0) { 93} 94Access to non-public constructor of class C 95Access to non-public constructor of class D 96object(E)#%d (0) { 97} 98Class E does not have a constructor, so you cannot pass any constructor arguments