File tree Expand file tree Collapse file tree 6 files changed +99
-9
lines changed
reflectionfunctionabstract Expand file tree Collapse file tree 6 files changed +99
-9
lines changed Original file line number Diff line number Diff line change 14
14
<void />
15
15
</methodsynopsis >
16
16
<para >
17
- Get the parameters as an array of <type >ReflectionParameter</type >.
17
+ Get the parameters as an array of <type >ReflectionParameter</type >,
18
+ in the order in which they are defined in the source.
18
19
</para >
19
20
20
21
&warn.undocumented.func;
Original file line number Diff line number Diff line change 14
14
<void />
15
15
</methodsynopsis >
16
16
<para >
17
- Checks if the type is a built-in type in PHP.
17
+ Checks if the type is a built-in type in PHP. A built-in type is any type that
18
+ is not a class, interface, or trait.
18
19
</para >
19
20
</refsect1 >
20
21
Original file line number Diff line number Diff line change 19
19
<para >
20
20
Gets the class type hinted for the parameter as a <classname >ReflectionClass</classname > object.
21
21
</para >
22
+ <para >
23
+ As of PHP 8.0.0 this function is deprecated and not recommended. Instead, use
24
+ <methodname >ReflectionParameter::getType</methodname > to get the <classname >ReflectionType</classname >
25
+ of the parameter, then interrogate that object to determine the parameter type.
26
+ </para >
22
27
23
28
&warn.undocumented.func;
24
29
36
41
or the declared type is not a class or interface.
37
42
</para >
38
43
</refsect1 >
39
-
44
+
40
45
<refsect1 role =" examples" >
41
46
&reftitle.examples;
42
47
<para >
@@ -55,12 +60,6 @@ echo $aParameter->getClass()->name;
55
60
?>
56
61
]]>
57
62
</programlisting >
58
- &example.outputs;
59
- <screen >
60
- <![CDATA[
61
- Exception
62
- ]]>
63
- </screen >
64
63
</example >
65
64
</para >
66
65
</refsect1 >
Original file line number Diff line number Diff line change 92
92
]]>
93
93
</screen >
94
94
</example >
95
+ <example >
96
+ <title ><methodname >ReflectionParameter::getType</methodname > Usage in PHP 8.0.0 and later</title >
97
+ <para >
98
+ As of PHP 8.0.0, this method may return a <classname >ReflectionNamedType</classname > instance or
99
+ a <classname >ReflectionUnionType</classname > instance. The latter is a collection of the former.
100
+ To analyze a type, it is often convenient to normalize it to an array of <classname >ReflectionNamedType</classname >
101
+ objects. The following function will return an array of <literal >0</literal > or more <classname >ReflectionNamedType</classname >
102
+ instances.
103
+ </para >
104
+ <programlisting role =" php" >
105
+ <![CDATA[
106
+ <?php
107
+ function getAllTypes(ReflectionParameter $reflectionParameter): array
108
+ {
109
+ $reflectionType = $reflectionParameter->getType();
110
+
111
+ if (!$reflectionType) return [];
112
+
113
+ return $reflectionType instanceof ReflectionUnionType
114
+ ? $reflectionType->getTypes()
115
+ : [$reflectionType];
116
+ }
117
+ ?>
118
+ ]]>
119
+ </programlisting >
120
+ </example >
95
121
</para >
96
122
</refsect1 >
97
123
Original file line number Diff line number Diff line change 9
9
10
10
<refsynopsisdiv >
11
11
&warn.deprecated.function-8-0-0;
12
+ <para >See the example below for an alternative way to derive this information.</para >
12
13
</refsynopsisdiv >
13
14
14
15
<refsect1 role =" description" >
34
35
</para >
35
36
</refsect1 >
36
37
38
+ <refsect1 role =" examples" >
39
+ &reftitle.examples;
40
+ <para >
41
+ <example >
42
+ <title >PHP 8.0.0 equivalent</title >
43
+ <para >
44
+ As of PHP 8.0.0, the following code will report if a type declares arrays,
45
+ including as part of a union.
46
+ </para >
47
+ <programlisting role =" php" >
48
+ <![CDATA[
49
+ <?php
50
+ function declaresArray(ReflectionParameter $reflectionParameter): bool
51
+ {
52
+ $reflectionType = $reflectionParameter->getType();
53
+
54
+ if (!$reflectionType) return false;
55
+
56
+ $types = $reflectionType instanceof ReflectionUnionType
57
+ ? $reflectionType->getTypes()
58
+ : [$reflectionType];
59
+
60
+ return in_array('array', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
61
+ }
62
+ ?>
63
+ ]]>
64
+ </programlisting >
65
+ </example >
66
+ </para >
67
+ </refsect1 >
68
+
37
69
<refsect1 role =" seealso" >
38
70
&reftitle.seealso;
39
71
<para >
Original file line number Diff line number Diff line change 9
9
10
10
<refsynopsisdiv >
11
11
&warn.deprecated.function-8-0-0;
12
+ <para >See the example below for an alternative way to derive this information.</para >
12
13
</refsynopsisdiv >
13
14
14
15
<refsect1 role =" description" >
38
39
</para >
39
40
</refsect1 >
40
41
42
+ <refsect1 role =" examples" >
43
+ &reftitle.examples;
44
+ <para >
45
+ <example >
46
+ <title >PHP 8.0.0 equivalent</title >
47
+ <para >
48
+ As of PHP 8.0.0, the following code will report if a type supports callables,
49
+ including as part of a union.
50
+ </para >
51
+ <programlisting role =" php" >
52
+ <![CDATA[
53
+ <?php
54
+ function declaresCallable(ReflectionParameter $reflectionParameter): bool
55
+ {
56
+ $reflectionType = $reflectionParameter->getType();
57
+
58
+ if (!$reflectionType) return false;
59
+
60
+ $types = $reflectionType instanceof ReflectionUnionType
61
+ ? $reflectionType->getTypes()
62
+ : [$reflectionType];
63
+
64
+ return in_array('callable', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
65
+ }
66
+ ?>
67
+ ]]>
68
+ </programlisting >
69
+ </example >
70
+ </para >
71
+ </refsect1 >
41
72
42
73
</refentry >
43
74
You can’t perform that action at this time.
0 commit comments