When building tools that inspect PHP code, ReflectionNamedType::isBuiltin() helps answer a focused question: distinguishes built-in PHP types from user classes or interfaces.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionNamedType::isBuiltin()?
Distinguishes built-in PHP types from user classes or interfaces.
Its method signature is:
public function isBuiltin(): bool
This method has no required arguments.
Return value: bool
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 $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->isBuiltin();
var_dump($result);
Output:
bool(true)
Explanation: Distinguishes built-in PHP types from user classes or interfaces.
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(string $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->isBuiltin();
var_dump($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
bool(true)
Result type: bool
Explanation: A class named DateTime is not a built-in scalar type.
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 $value): void {}
$typeReflection = (new ReflectionFunction("inspectValue"))->getParameters()[0]->getType();
$result = $typeReflection->isBuiltin();
var_dump($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:
bool(false)
Status: no
Explanation: getName supplies the type name being classified.
Important Behavior and Edge Cases
- A class named DateTime is not a built-in scalar type.
- getName supplies the type name being classified.
- 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 |
|---|---|
ReflectionNamedType::isBuiltin() | Distinguishes built-in PHP types from user classes or interfaces. |
Related API | getName supplies the type name being classified. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: A class named DateTime is not a built-in scalar type. | Handle the specific limitation: A class named DateTime is not a built-in scalar type. |
| Confusing this method with Related API | getName supplies the type name being classified. |
Key Takeaways
- Distinguishes built-in PHP types from user classes or interfaces.
- A class named DateTime is not a built-in scalar type.
- getName supplies the type name being classified.
Use ReflectionNamedType::isBuiltin() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.