ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isInstance in PHP?
Reports whether an object is an instance of the reflected class, including subclasses.
ReflectionClass::isInstance(object $object)
Parameters:
$object— Required. An object to test or a lazy instance controlled by this ReflectionClass.
Example 1: Understanding the Basic Behavior
<?php
class Report { public const VERSION = 2; public string $title = 'Draft'; protected int $pages = 10; public static string $category = 'docs'; public function publish(): void {} }
$reflectionClass = new ReflectionClass(Report::class);
$result = $reflectionClass->isInstance(new Report());
var_dump($result);
Output:
bool(true)
Explanation: Reports whether an object is an instance of the reflected class, including subclasses.
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->isInstance(new Invoice());
var_dump($result);
Output:
bool(true)
Explanation: Subclass instances satisfy an isInstance() test for their parent type.
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->isInstance(new CompletedJob());
var_dump($result);
Output:
bool(true)
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Subclass instances satisfy an isInstance() test for their parent type.
- 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::isInstance | Reports whether an object is an instance of the reflected class, including subclasses. |
is_a() | isInstance() checks an object against the reflected class; is_a() performs a similar check using a class-name argument. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isInstance() with an invocation of the class itself. | Subclass instances satisfy an isInstance() test for their parent type. |
| Choosing is_a() without checking whether it provides the same result. | isInstance() checks an object against the reflected class; is_a() performs a similar check using a class-name argument. |
Key Takeaways
- Reports whether an object is an instance of the reflected class, including subclasses.
- Subclass instances satisfy an isInstance() test for their parent type.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isInstance fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.