Content · Web Development · PHP

PHP ReflectionIntersectionType::getTypes() Method: Usage and Examples

Returns the individual class/interface ReflectionNamedType parts of an intersection type.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

When building tools that inspect PHP code, ReflectionIntersectionType::getTypes() helps answer a focused question: returns the individual class/interface ReflectionNamedType parts of an intersection type.

Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.

What Is ReflectionIntersectionType::getTypes()?


Returns the individual class/interface ReflectionNamedType parts of an intersection type.

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 receiveItems(Iterator&Countable $items): void {}
$parameterReflection = (new ReflectionFunction('receiveItems'))->getParameters()[0];
$intersection = $parameterReflection->getType();

$result = $intersection->getTypes();
echo implode(" & ", array_map(static fn($type) => $type->getName(), $result));

Output:

Iterator & Countable

Explanation: Returns the individual class/interface ReflectionNamedType parts of an intersection type.

Example 2: Compare a second case


Next, change the reflected declaration or parameter to observe a related situation.

<?php

declare(strict_types=1);

function receiveItems(Iterator&Countable $items): void {}
$parameterReflection = (new ReflectionFunction('receiveItems'))->getParameters()[0];
$intersection = $parameterReflection->getType();

$result = $intersection->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:

Iterator & Countable
Result type: array

Explanation: Every value must satisfy every intersected type; union types mean something different.

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 receiveItems(Iterator&Countable $items): void {}
$parameterReflection = (new ReflectionFunction('receiveItems'))->getParameters()[0];
$intersection = $parameterReflection->getType();

$result = $intersection->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:

Iterator & Countable
Entries: 2

Explanation: ReflectionUnionType::getTypes returns alternatives instead.

Important Behavior and Edge Cases


  • Every value must satisfy every intersected type; union types mean something different.
  • ReflectionUnionType::getTypes returns alternatives instead.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionIntersectionType::getTypes()Returns the individual class/interface ReflectionNamedType parts of an intersection type.
Related APIReflectionUnionType::getTypes returns alternatives instead.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Every value must satisfy every intersected type; union types mean something different.Handle the specific limitation: Every value must satisfy every intersected type; union types mean something different.
Confusing this method with Related APIReflectionUnionType::getTypes returns alternatives instead.

Key Takeaways


  • Returns the individual class/interface ReflectionNamedType parts of an intersection type.
  • Every value must satisfy every intersected type; union types mean something different.
  • ReflectionUnionType::getTypes returns alternatives instead.

Use ReflectionIntersectionType::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.

Share this resourceSend this page to someone who may find it useful.