Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection deprecation #578

Merged
merged 11 commits into from May 6, 2021
Expand Up @@ -14,7 +14,8 @@
<void />
</methodsynopsis>
<para>
Get the parameters as an array of <type>ReflectionParameter</type>.
Get the parameters as an array of <type>ReflectionParameter</type>,
in the order in which they are defined in the source.
Crell marked this conversation as resolved.
Show resolved Hide resolved
</para>

&warn.undocumented.func;
Expand Down
3 changes: 2 additions & 1 deletion reference/reflection/reflectionnamedtype/isbuiltin.xml
Expand Up @@ -14,7 +14,8 @@
<void />
</methodsynopsis>
<para>
Checks if the type is a built-in type in PHP.
Checks if the type is a built-in type in PHP. A built-in type is any type that
is not a class, interface, or trait.
</para>
</refsect1>

Expand Down
8 changes: 7 additions & 1 deletion reference/reflection/reflectionparameter/getclass.xml
Expand Up @@ -36,7 +36,13 @@
or the declared type is not a class or interface.
</para>
</refsect1>


<section>
As of PHP 8.0.0 this function is deprecated and not recommended. Instead, use
<methodname>ReflectionParameter::getType</methodname> to get the <class>ReflectionType</class>
of the parameter, then interrogate that object to determine the parameter type.
</section>
Crell marked this conversation as resolved.
Show resolved Hide resolved

<refsect1 role="examples">
&reftitle.examples;
<para>
Expand Down
25 changes: 25 additions & 0 deletions reference/reflection/reflectionparameter/gettype.xml
Expand Up @@ -92,6 +92,31 @@ NULL
]]>
</screen>
</example>
<example>
<title><methodname>ReflectionParameter::getType</methodname> Usage in PHP 8.0.0 and later</title>
<para>
As of PHP 8.0.0, this method may return a <classname>ReflectionNamedType</classname> instance or
a <classname>ReflectionUnionType</classname> instance. The latter is a collection of the former.
Crell marked this conversation as resolved.
Show resolved Hide resolved
To analyze a type, it is often convenient to normalize it to an array of <classname>ReflectionNamedType</classname>
objects. The following function will return an array of <literal>0</literal> or more <classname>ReflectionNamedType</classname>
instances.
</para>
<programlisting role="php">
<![CDATA[
<?php
function getAllTypes(ReflectionParameter $reflectionParameter): array {
Crell marked this conversation as resolved.
Show resolved Hide resolved
$reflectionType = $reflectionParameter->getType();

if (!$reflectionType) return [];

return $reflectionType instanceof ReflectionUnionType
? $reflectionType->getTypes()
: [$reflectionType];
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>

Expand Down
26 changes: 26 additions & 0 deletions reference/reflection/reflectionparameter/isarray.xml
Expand Up @@ -34,6 +34,32 @@
</para>
</refsect1>

<section>
As of PHP 8.0.0, the following code will report if a type supports arrays,
including as part of a union.
</section>
Crell marked this conversation as resolved.
Show resolved Hide resolved

<example>
<title>PHP 8.0.0 equivalent</title>
<programlisting role="php">
<![CDATA[
<?php
function allowsArray(ReflectionParameter $reflectionParameter): bool {
$reflectionType = $reflectionParameter->getType();

if (!$reflectionType) return false;
Crell marked this conversation as resolved.
Show resolved Hide resolved

$types = $reflectionType instanceof ReflectionUnionType
? $reflectionType->getTypes()
: [$reflectionType];

return in_array('array', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
}
?>
]]>
</programlisting>
</example>

<refsect1 role="seealso">
&reftitle.seealso;
<para>
Expand Down
25 changes: 25 additions & 0 deletions reference/reflection/reflectionparameter/iscallable.xml
Expand Up @@ -38,6 +38,31 @@
</para>
</refsect1>

<section>
As of PHP 8.0.0, the following code will report if a type supports callables,
including as part of a union.
</section>
Crell marked this conversation as resolved.
Show resolved Hide resolved

<example>
<title>PHP 8.0.0 equivalent</title>
<programlisting role="php">
<![CDATA[
<?php
function allowsCallable(ReflectionParameter $reflectionParameter): bool {
$reflectionType = $reflectionParameter->getType();

if (!$reflectionType) return false;
Crell marked this conversation as resolved.
Show resolved Hide resolved

$types = $reflectionType instanceof ReflectionUnionType
? $reflectionType->getTypes()
: [$reflectionType];

return in_array('callable', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
}
?>
]]>
</programlisting>
</example>

</refentry>

Expand Down