php8.5
Home/ Manual/ reflection / reflectionuniontype/ ReflectionUnionType::getTypes

ReflectionUnionType::getTypes

PHP function Edit on GitHub ✎

(PHP 8)

Returns the types included in the union type

Description

ReflectionUnionType::getTypes(): array

Returns the reflections of types included in the union type.

Parameters Parameters

Return Values

An array of ReflectionType objects.

Examples

ReflectionUnionType::getTypes example

php
<?php
function someFunction(int|float $number) {}

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

var_dump($reflectionParam->getType()->getTypes());

The above example will output something similar to:

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

See Also

  • ReflectionType::allowsNull
  • ReflectionParameter::getType

Source: reference/reflection/reflectionuniontype/gettypes.xml · from the official PHP manual (php/doc-en)