PropertyReflector
Raxos\Reflection\PropertyReflector
Reflects a single class property. It can read, write and unset the property on a given instance, report its visibility and modifiers, and introspect its type, including the element type declared in an @var doc comment. It uses the Attributable trait for attribute reading.
final readonly class PropertyReflector implements ReflectorInterfaceThe class implements the ReflectorInterface contract from raxos/contract.
Methods
__construct()
public function __construct(ReflectionProperty $property)Creates a reflector from a native ReflectionProperty. You usually obtain one from a ClassReflector.
accepts()
public function accepts(mixed $input): boolChecks whether a value is valid for the property's declared type, delegating to TypeReflector.
getValue()
public function getValue(object $instance, mixed $default = null): mixedReads the property value from an instance. When the value is null or the property is not yet accessible, the default is returned instead.
setValue()
public function setValue(object $instance, mixed $value): voidSets the property value on an instance.
unsetValue()
public function unsetValue(object $instance): voidUnsets the property on an instance.
isInitialized()
public function isInitialized(object $instance): boolChecks whether the property has been initialized on the instance.
getClass()
public function getClass(): ClassReflectorReturns a ClassReflector for the declaring class.
getType()
public function getType(): TypeReflectorReturns a TypeReflector for the property's declared type.
getName()
public function getName(): stringReturns the property name.
getDefaultValue()
public function getDefaultValue(): mixedReturns the declared default value.
hasDefaultValue()
public function hasDefaultValue(): boolChecks whether the property has a default value. For promoted properties, it also inspects the matching constructor parameter.
hasType()
public function hasType(): boolChecks whether the property has a declared type.
isIterable()
public function isIterable(): boolChecks whether the property's type is iterable.
isPromoted()
public function isPromoted(): boolChecks whether the property is a promoted constructor parameter.
isNullable()
public function isNullable(): boolChecks whether the property's type allows null.
isPublic()
public function isPublic(): boolChecks whether the property is public.
isProtected()
public function isProtected(): boolChecks whether the property is protected.
isPrivate()
public function isPrivate(): boolChecks whether the property is private.
isReadonly()
public function isReadonly(): boolChecks whether the property is readonly.
isVirtual()
public function isVirtual(): boolChecks whether the property is virtual.
getIterableType()
public function getIterableType(): ?TypeReflectorReads the element type from an @var Type[] style doc comment, if present, and returns it as a TypeReflector.
Usage
<?php
declare(strict_types=1);
use function Raxos\Reflection\reflect;
$property = reflect(User::class)->getProperty('name');
if (!$property->isInitialized($user)) {
$property->setValue($user, 'Bas');
}
echo $property->getValue($user);