When building tools that inspect PHP code, ReflectionProperty::setRawValue() helps answer a focused question: sets a backed property's raw value without calling its set hook.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionProperty::setRawValue()?
Sets a backed property's raw value without calling its set hook.
Its method signature is:
public function setRawValue(object $object, mixed $value): void
Parameters:
$object— Object containing the property to read or update (not needed for a static property) Required.$value— New value; it must satisfy the declared property type Required.
Return value: void
Raw property access and lazy-object controls can bypass normal hooks or initialization. These APIs are intended for controlled framework and persistence tasks.
Example 1: Inspect a property declaration
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
#[FieldLabel('identity')]
public string $name = 'Morgan';
public private(set) string $token = 'abc';
public protected(set) int $level = 1;
public readonly int $identifier;
public static int $counter = 3;
public string $label { get => strtoupper($this->name); }
public string $initials { get => substr($this->name, 0, 1); }
final public string $finalLabel { get => $this->name; }
public int $delayed;
public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "name");
$customer = new CustomerData("Oslo");
$result = $propertyReflection->setRawValue($customer, "Taylor");
echo $propertyReflection->getRawValue($customer);
Output:
Taylor
Explanation: Sets a backed property's raw value without calling its set hook.
Example 2: Explore another property context
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
#[FieldLabel('identity')]
public string $name = 'Morgan';
public private(set) string $token = 'abc';
public protected(set) int $level = 1;
public readonly int $identifier;
public static int $counter = 3;
public string $label { get => strtoupper($this->name); }
public string $initials { get => substr($this->name, 0, 1); }
final public string $finalLabel { get => $this->name; }
public int $delayed;
public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "token");
$customer = new CustomerData("Oslo");
$result = $propertyReflection->setRawValue($customer, "Taylor");
echo $propertyReflection->getRawValue($customer);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
Taylor
Result type: null
Explanation: Raw writes bypass validation logic and should not be used for ordinary user input.
Practical Example: Apply reflection to a data model
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
#[FieldLabel('identity')]
public string $name = 'Morgan';
public private(set) string $token = 'abc';
public protected(set) int $level = 1;
public readonly int $identifier;
public static int $counter = 3;
public string $label { get => strtoupper($this->name); }
public string $initials { get => substr($this->name, 0, 1); }
final public string $finalLabel { get => $this->name; }
public int $delayed;
public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "level");
$customer = new CustomerData("Oslo");
$result = $propertyReflection->setRawValue($customer, 6);
echo $propertyReflection->getRawValue($customer);
// 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:
6
No value returned
Explanation: setValue uses normal set semantics and can invoke a hook.
Important Behavior and Edge Cases
- Raw writes bypass validation logic and should not be used for ordinary user input.
- setValue uses normal set semantics and can invoke a hook.
- 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 |
|---|---|
ReflectionProperty::setRawValue() | Sets a backed property's raw value without calling its set hook. |
Related API | setValue uses normal set semantics and can invoke a hook. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Raw writes bypass validation logic and should not be used for ordinary user input. | Handle the specific limitation: Raw writes bypass validation logic and should not be used for ordinary user input. |
| Confusing this method with Related API | setValue uses normal set semantics and can invoke a hook. |
Key Takeaways
- Sets a backed property's raw value without calling its set hook.
- Raw writes bypass validation logic and should not be used for ordinary user input.
- setValue uses normal set semantics and can invoke a hook.
Use ReflectionProperty::setRawValue() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.