ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::inNamespace in PHP?
Reports whether the reflected class belongs to a named namespace.
ReflectionClass::inNamespace()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
namespace Guide\Catalog;
class Product {}
$reflectionClass = new \ReflectionClass(Product::class);
var_dump($reflectionClass->inNamespace());
Output:
bool(true)
Explanation: Reports whether the reflected class belongs to a named namespace.
Example 2: Testing Another Case
<?php
interface Exportable { public function export(): string; }
trait HasId { public function getId(): int { return 12; } }
class Invoice implements Exportable { use HasId; public string $name = 'Invoice'; public const STATE = 'open'; public function export(): string { return 'invoice'; } }
$reflectionClass = new ReflectionClass(Invoice::class);
$result = $reflectionClass->inNamespace();
var_dump($result);
Output:
bool(false)
Explanation: This checks whether the reflected class belongs to a namespace, not whether a namespace alias was imported.
Practical Example: Applying the Operation
<?php
abstract class BaseJob { protected string $name = 'job'; abstract public function run(): void; }
class CompletedJob extends BaseJob { public function run(): void {} }
$reflectionClass = new ReflectionClass(CompletedJob::class);
$result = $reflectionClass->inNamespace();
var_dump($result);
Output:
bool(false)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- This checks whether the reflected class belongs to a namespace, not whether a namespace alias was imported.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClass::inNamespace | Reports whether the reflected class belongs to a named namespace. |
ReflectionClass::getNamespaceName() | inNamespace() returns a boolean; getNamespaceName() returns the namespace string. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using the returned Reflection data without considering absent declarations or internal classes. | This checks whether the reflected class belongs to a namespace, not whether a namespace alias was imported. |
| Choosing ReflectionClass::getNamespaceName() without checking whether it provides the same result. | inNamespace() returns a boolean; getNamespaceName() returns the namespace string. |
Key Takeaways
- Reports whether the reflected class belongs to a named namespace.
- This checks whether the reflected class belongs to a namespace, not whether a namespace alias was imported.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::inNamespace fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.