ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::isCloneable in PHP?
Reports whether instances can be cloned.
ReflectionClass::isCloneable()
This operation does not accept method arguments.
Example 1: Understanding the Basic Behavior
<?php
class RestrictedRecord { private function __clone() {} }
$reflectionClass = new ReflectionClass(RestrictedRecord::class);
var_dump($reflectionClass->isCloneable());
Output:
bool(false)
Explanation: Reports whether instances can be cloned.
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->isCloneable();
var_dump($result);
Output:
bool(true)
Explanation: A private or protected clone method can prevent code outside the class from cloning an instance.
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->isCloneable();
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
- A private or protected clone method can prevent code outside the class from cloning an instance.
- 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::isCloneable | Reports whether instances can be cloned. |
ReflectionClass::isInstantiable() | isCloneable() tests whether instances can be cloned; isInstantiable() tests whether they can be constructed. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing a class-declaration check by ReflectionClass::isCloneable() with an invocation of the class itself. | A private or protected clone method can prevent code outside the class from cloning an instance. |
| Choosing ReflectionClass::isInstantiable() without checking whether it provides the same result. | isCloneable() tests whether instances can be cloned; isInstantiable() tests whether they can be constructed. |
Key Takeaways
- Reports whether instances can be cloned.
- A private or protected clone method can prevent code outside the class from cloning an instance.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::isCloneable fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.