When building tools that inspect PHP code, ReflectionProperty::getDefaultValue() helps answer a focused question: reads a property's declared default value without requiring an object.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionProperty::getDefaultValue()?
Reads a property's declared default value without requiring an object.
Its method signature is:
public function getDefaultValue(): mixed
This method has no required arguments.
Return value: mixed
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");
$result = $propertyReflection->getDefaultValue();
var_dump($result);
Output:
string(6) "Morgan"
Explanation: Reads a property's declared default value without requiring an object.
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");
$result = $propertyReflection->getDefaultValue();
var_dump($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
string(3) "abc"
Result type: string
Explanation: Properties lacking an initialized default may yield null but are not necessarily initialized on an object.
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");
$result = $propertyReflection->getDefaultValue();
var_dump($result);
// 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:
int(1)
Reflection result available
Explanation: hasDefaultValue checks whether the property explicitly has a default.
Important Behavior and Edge Cases
- Properties lacking an initialized default may yield null but are not necessarily initialized on an object.
- hasDefaultValue checks whether the property explicitly has a default.
- 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::getDefaultValue() | Reads a property's declared default value without requiring an object. |
Related API | hasDefaultValue checks whether the property explicitly has a default. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Properties lacking an initialized default may yield null but are not necessarily initialized on an object. | Handle the specific limitation: Properties lacking an initialized default may yield null but are not necessarily initialized on an object. |
| Confusing this method with Related API | hasDefaultValue checks whether the property explicitly has a default. |
Key Takeaways
- Reads a property's declared default value without requiring an object.
- Properties lacking an initialized default may yield null but are not necessarily initialized on an object.
- hasDefaultValue checks whether the property explicitly has a default.
Use ReflectionProperty::getDefaultValue() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.