When building tools that inspect PHP code, ReflectionUnionType::getTypes() helps answer a focused question: returns the constituent types of a union declaration such as int|string.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionUnionType::getTypes()?
Returns the constituent types of a union declaration such as int|string.
Its method signature is:
public function getTypes(): array
This method has no required arguments.
Return value: array
Example 1: Inspect the basic behavior
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
function inspectValue(int|string $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->getTypes();
echo implode(" | ", array_map(static fn($type) => $type->getName(), $result));
Output:
string | int
Explanation: Returns the constituent types of a union declaration such as int|string.
Example 2: Compare a second case
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
function inspectValue(float|bool $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->getTypes();
echo implode(" | ", array_map(static fn($type) => $type->getName(), $result));
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
float | bool
Result type: array
Explanation: A union accepts any member type; an intersection requires all members.
Practical Example: Apply the operation in context
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
function inspectValue(DateTime|DateTimeImmutable $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->getTypes();
echo implode(" | ", array_map(static fn($type) => $type->getName(), $result));
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
DateTime | DateTimeImmutable
Entries: 2
Explanation: ReflectionIntersectionType::getTypes reads intersection members.
Important Behavior and Edge Cases
- A union accepts any member type; an intersection requires all members.
- ReflectionIntersectionType::getTypes reads intersection members.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionUnionType::getTypes() | Returns the constituent types of a union declaration such as int|string. |
Related API | ReflectionIntersectionType::getTypes reads intersection members. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: A union accepts any member type; an intersection requires all members. | Handle the specific limitation: A union accepts any member type; an intersection requires all members. |
| Confusing this method with Related API | ReflectionIntersectionType::getTypes reads intersection members. |
Key Takeaways
- Returns the constituent types of a union declaration such as int|string.
- A union accepts any member type; an intersection requires all members.
- ReflectionIntersectionType::getTypes reads intersection members.
Use ReflectionUnionType::getTypes() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.