xref: /PHP-5.5/ext/reflection/tests/007.phpt (revision 610c7fbe)
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
29	echo "====>newInstance(25)\n";
30	try
31	{
32		var_dump($ref->newInstance(25));
33	}
34	catch (ReflectionException $e)
35	{
36		var_dump($e->getMessage());
37	}
38
39	echo "====>newInstance(25, 42)\n";
40	try
41	{
42		var_dump($ref->newInstance(25, 42));
43	}
44	catch (ReflectionException $e)
45	{
46		var_dump($e->getMessage());
47	}
48
49	echo "\n";
50}
51
52function __autoload($class)
53{
54	echo __FUNCTION__ . "($class)\n";
55}
56
57test('Class_does_not_exist');
58
59Class NoCtor
60{
61}
62
63test('NoCtor');
64
65Class WithCtor
66{
67	function __construct()
68	{
69		echo __METHOD__ . "()\n";
70		var_dump(func_get_args());
71	}
72}
73
74test('WithCtor');
75
76Class WithCtorWithArgs
77{
78	function __construct($arg)
79	{
80		echo __METHOD__ . "($arg)\n";
81		var_dump(func_get_args());
82	}
83}
84
85test('WithCtorWithArgs');
86
87?>
88===DONE===
89<?php exit(0); ?>
90--EXPECTF--
91
92====>Class_does_not_exist
93__autoload(Class_does_not_exist)
94string(41) "Class Class_does_not_exist does not exist"
95====>NoCtor
96====>newInstance()
97object(NoCtor)#%d (0) {
98}
99====>newInstance(25)
100string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
101====>newInstance(25, 42)
102string(86) "Class NoCtor does not have a constructor, so you cannot pass any constructor arguments"
103
104====>WithCtor
105====>newInstance()
106WithCtor::__construct()
107array(0) {
108}
109object(WithCtor)#%d (0) {
110}
111====>newInstance(25)
112WithCtor::__construct()
113array(1) {
114  [0]=>
115  int(25)
116}
117object(WithCtor)#%d (0) {
118}
119====>newInstance(25, 42)
120WithCtor::__construct()
121array(2) {
122  [0]=>
123  int(25)
124  [1]=>
125  int(42)
126}
127object(WithCtor)#%d (0) {
128}
129
130====>WithCtorWithArgs
131====>newInstance()
132
133Warning: Missing argument 1 for WithCtorWithArgs::__construct() in %s007.php on line %d
134
135Notice: Undefined variable: arg in %s007.php on line %d
136WithCtorWithArgs::__construct()
137array(0) {
138}
139object(WithCtorWithArgs)#%d (0) {
140}
141====>newInstance(25)
142WithCtorWithArgs::__construct(25)
143array(1) {
144  [0]=>
145  int(25)
146}
147object(WithCtorWithArgs)#%d (0) {
148}
149====>newInstance(25, 42)
150WithCtorWithArgs::__construct(25)
151array(2) {
152  [0]=>
153  int(25)
154  [1]=>
155  int(42)
156}
157object(WithCtorWithArgs)#%d (0) {
158}
159
160===DONE===
161