xref: /php-src/ext/reflection/tests/007.phpt (revision 08b2ab22)
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
56spl_autoload_register(function ($class) {
57    echo __FUNCTION__ . "($class)\n";
58});
59
60test('Class_does_not_exist');
61
62Class NoCtor
63{
64}
65
66test('NoCtor');
67
68Class WithCtor
69{
70    function __construct()
71    {
72        echo __METHOD__ . "()\n";
73        var_dump(func_get_args());
74    }
75}
76
77test('WithCtor');
78
79Class WithCtorWithArgs
80{
81    function __construct($arg)
82    {
83        echo __METHOD__ . "($arg)\n";
84        var_dump(func_get_args());
85    }
86}
87
88test('WithCtorWithArgs');
89
90?>
91--EXPECTF--
92====>Class_does_not_exist
93{closure:%s:%d}(Class_does_not_exist)
94string(43) "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()
132Exception: Too few arguments to function WithCtorWithArgs::__construct(), 0 passed and exactly 1 expected
133====>newInstance(25)
134WithCtorWithArgs::__construct(25)
135array(1) {
136  [0]=>
137  int(25)
138}
139object(WithCtorWithArgs)#%d (0) {
140}
141====>newInstance(25, 42)
142WithCtorWithArgs::__construct(25)
143array(2) {
144  [0]=>
145  int(25)
146  [1]=>
147  int(42)
148}
149object(WithCtorWithArgs)#%d (0) {
150}
151