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 45try { 46 var_dump($rcB->newInstance()); 47} catch (Throwable $e) { 48 echo "Exception: " . $e->getMessage() . "\n"; 49} 50try { 51 var_dump($rcB->newInstance('x', 123)); 52} catch (Throwable $e) { 53 echo "Exception: " . $e->getMessage() . "\n"; 54} 55 56try { 57 $rcC->newInstance(); 58 echo "you should not see this\n"; 59} catch (Exception $e) { 60 echo $e->getMessage() . "\n"; 61} 62 63try { 64 $rcD->newInstance(); 65 echo "you should not see this\n"; 66} catch (Exception $e) { 67 echo $e->getMessage() . "\n"; 68} 69 70$e1 = $rcE->newInstance(); 71var_dump($e1); 72 73try { 74 $e2 = $rcE->newInstance('x'); 75 echo "you should not see this\n"; 76} catch (Exception $e) { 77 echo $e->getMessage() . "\n"; 78} 79?> 80--EXPECTF-- 81Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; A has a deprecated constructor in %s on line %d 82In constructor of class A 83In constructor of class A 84object(A)#%d (0) { 85} 86object(A)#%d (0) { 87} 88Exception: Too few arguments to function B::__construct(), 0 passed and exactly 2 expected 89In constructor of class B with args x, 123 90object(B)#%d (0) { 91} 92Access to non-public constructor of class C 93Access to non-public constructor of class D 94object(E)#%d (0) { 95} 96Class E does not have a constructor, so you cannot pass any constructor arguments 97