ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isInstantiable in PHP?
Reports whether the class can be instantiated, excluding interfaces and abstract classes.
ReflectionClass::isInstantiable()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
abstract class AbstractTask { abstract public function run(): void; }
$reflectionClass = new ReflectionClass(AbstractTask::class);
var_dump($reflectionClass->isInstantiable());
Output:
bool(false)
Explanation: Reports whether the class can be instantiated, excluding interfaces and abstract classes.
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->isInstantiable();
var_dump($result);
Output:
bool(true)
Explanation: Interfaces and abstract classes are not instantiable, regardless of available constructors.
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->isInstantiable();
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
- Interfaces and abstract classes are not instantiable, regardless of available constructors.
- 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::isInstantiable | Reports whether the class can be instantiated, excluding interfaces and abstract classes. |
ReflectionClass::isAbstract() | isInstantiable() considers whether the type can be created, not just whether it has the abstract modifier. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isInstantiable() with an invocation of the class itself. | Interfaces and abstract classes are not instantiable, regardless of available constructors. |
| Choosing ReflectionClass::isAbstract() without checking whether it provides the same result. | isInstantiable() considers whether the type can be created, not just whether it has the abstract modifier. |
Key Takeaways
- Reports whether the class can be instantiated, excluding interfaces and abstract classes.
- Interfaces and abstract classes are not instantiable, regardless of available constructors.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isInstantiable fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.