ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getStaticProperties in PHP?
Returns currently defined static property names and values.
ReflectionClass::getStaticProperties()
This operation does not accept method arguments.
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->getStaticProperties();
var_dump($result);
Output:
array(1) {
["category"]=>
string(4) "docs"
}
Explanation: Returns currently defined static property names and values.
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->getStaticProperties();
var_dump($result);
Output:
array(0) {
}
Explanation: Only static properties are returned; instance property values require a separate object.
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->getStaticProperties();
var_dump($result);
Output:
array(0) {
}
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- Only static properties are returned; instance property values require a separate object.
- 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::getStaticProperties | Returns currently defined static property names and values. |
ReflectionClass::getStaticPropertyValue() | getStaticProperties() returns all static property values; getStaticPropertyValue() selects one. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getStaticProperties() as though it executes the reflected class member. | Only static properties are returned; instance property values require a separate object. |
| Choosing ReflectionClass::getStaticPropertyValue() without checking whether it provides the same result. | getStaticProperties() returns all static property values; getStaticPropertyValue() selects one. |
Key Takeaways
- Returns currently defined static property names and values.
- Only static properties are returned; instance property values require a separate object.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getStaticProperties fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.