When building tools that inspect PHP code, ReflectionClass::setStaticPropertyValue() helps answer a focused question: changes a static property by its name through class reflection.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionClass::setStaticPropertyValue()?
Changes a static property by its name through class reflection.
Its method signature is:
public function setStaticPropertyValue(string $name, mixed $value): void
Parameters:
$name— Name of the static property being assigned Required.$value— New value for the named static property Required.
Return value: void
Example 1: Inspect a PHP class declaration
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
use HasIdentifier;
#[GuideTag('category')]
final public const string TYPE = 'parcel';
private const int SECRET = 5;
public string $title = 'Box';
public function __construct(public int $number = 10) {}
public function name(): string { return $this->title; }
public function getIterator(): Traversable { yield 'item' => $this->title; }
public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }
$classReflection = new ReflectionClass(Shipment::class);
$result = $classReflection->setStaticPropertyValue("total", 10);
echo Shipment::$total;
Output:
10
Explanation: Changes a static property by its name through class reflection.
Example 2: Compare a different class target
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
use HasIdentifier;
#[GuideTag('category')]
final public const string TYPE = 'parcel';
private const int SECRET = 5;
public string $title = 'Box';
public function __construct(public int $number = 10) {}
public function name(): string { return $this->title; }
public function getIterator(): Traversable { yield 'item' => $this->title; }
public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }
$classReflection = new ReflectionClass(Shipment::class);
$result = $classReflection->setStaticPropertyValue("total", 15);
echo Shipment::$total;
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
15
Result type: null
Explanation: The named property must exist and be static; typed declarations still restrict values.
Practical Example: Apply reflection to a real object model
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
use HasIdentifier;
#[GuideTag('category')]
final public const string TYPE = 'parcel';
private const int SECRET = 5;
public string $title = 'Box';
public function __construct(public int $number = 10) {}
public function name(): string { return $this->title; }
public function getIterator(): Traversable { yield 'item' => $this->title; }
public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }
$classReflection = new ReflectionClass(Shipment::class);
$result = $classReflection->setStaticPropertyValue("total", 20);
echo Shipment::$total;
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
20
No value returned
Explanation: ReflectionProperty::setValue can also target a static property.
Important Behavior and Edge Cases
- The named property must exist and be static; typed declarations still restrict values.
- ReflectionProperty::setValue can also target a static property.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClass::setStaticPropertyValue() | Changes a static property by its name through class reflection. |
Related API | ReflectionProperty::setValue can also target a static property. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: The named property must exist and be static; typed declarations still restrict values. | Handle the specific limitation: The named property must exist and be static; typed declarations still restrict values. |
| Confusing this method with Related API | ReflectionProperty::setValue can also target a static property. |
Key Takeaways
- Changes a static property by its name through class reflection.
- The named property must exist and be static; typed declarations still restrict values.
- ReflectionProperty::setValue can also target a static property.
Use ReflectionClass::setStaticPropertyValue() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.