PHP 8.1.28 Released!


(PHP 8)

ReflectionUnionType::getTypesReturns the types included in the union type


public ReflectionUnionType::getTypes(): array

Returns the reflections of types included in the union type.

Bağımsız Değişkenler

Bu işlevin bağımsız değişkeni yoktur.

Dönen Değerler

An array of ReflectionType objects.


Örnek 1 ReflectionUnionType::getTypes() example

function someFunction(int|float $number) {}

$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParam = $reflectionFunc->getParameters()[0];


Yukarıdaki örnek şuna benzer bir çıktı üretir:

array(2) {
    [0] =>
    class ReflectionNamedType#4(0) {
    [1] =>
    class ReflectionNamedType#5(0) {

Ayrıca Bakınız

add a note

User Contributed Notes 2 notes

baptiste at pillot dot fr
7 months ago
Sorting :

Contrary to what one might expect, the returned array of ReflectionType objects will not be sorted in the same order than the types declared into the source code.

- Classes, interfaces, traits, iterable (replaced by Traversable), ReflectionIntersectionType objects, parent and self: these types will be returned first, in the order in which they were declared.
- static and all built-in types (iterable replaced by array) will come next. They will always be returned in this order: static, callable, array, string, int, float, bool (or false or true), null.

Notice that when used into an union type, iterable is an alias for Traversable|array. ReflectionUnionType::getTypes will return these both ReflectionNamedType objects, instead of a single one named 'iterable'.

Example :
class PC {}
C extends PC {
f(): null|bool|float|int|parent|PC|string|iterable|(ReflectionClass&ReflectionProperty)|callable|static|self|C {}
join(', ', array_map(
$t) { return ($t instanceof ReflectionIntersectionType) ? '<intersection>' : $t->getName(); },
ReflectionMethod('C', 'f'))->getReturnType()->getTypes()
)) .

Will display :
parent, PC, Traversable, <intersection>, self, C, static, callable, array, string, int, float, bool, null

Try it:
baptiste at pillot dot fr
7 months ago
ReflectionUnionType::getTypes can return an array of ReflectionNamedType and/or ReflectionIntersectionType objects only.
To Top