Symfony 8.2 Adds Custom Property Accessors and Property Hook Metadata
- Javier Eguiluz
- 877 reads
Symfony 8.2 maps domain-specific methods to object properties and exposes hook metadata, improving access for forms, serializers and object mappers.

Symfony 8.2 makes object properties easier for framework components to work with when a class uses domain-specific method names or PHP property hooks. The change centers on PropertyInfo: it can now describe custom read and write methods, and its reflection metadata can report whether a property uses hooks or is virtual. That gives tools such as PropertyAccess and the serializer a shared account of how an object exposes its data.

Map domain methods to properties
Symfony has traditionally found access methods by convention. For a property such as name, it can recognize methods like getName() and setName(); for a collection, it can look for methods such as addRole() and removeRole(). That pattern is convenient when class APIs follow those names. It gets in the way when a domain model uses verbs that express its own rules, such as rename() or grant().
Before Symfony 8.2, making those methods visible to PropertyInfo required a custom extractor. The new #[WithAccessors] attribute lets a class declare the mapping on the property itself:
use Symfony\Component\PropertyInfo\Attribute\WithAccessors;
class User
{
#[WithAccessors(getter: 'displayName', setter: 'rename')]
private string $name;
#[WithAccessors(getter: 'grantedRoles', adder: 'grant', remover: 'revoke')]
private array $roles = [];
public function displayName(): string
{
return $this->name;
}
public function rename(string $name): void
{
$this->name = $name;
}
}The attribute's four arguments—getter, setter, adder and remover—are optional. Collection additions and removals have to be declared together, so Symfony knows how to reconcile a current collection with a new value. If a named method doesn't exist, Symfony raises a MappingException instead of quietly switching to its naming conventions. That makes a broken mapping visible when it is encountered.
Once the metadata is available, PropertyAccess can use the declared methods when a caller works with the property name:
$propertyAccessor->getValue($user, 'name'); // calls displayName()
$propertyAccessor->setValue($user, 'name', 'Jane Doe'); // calls rename()
$propertyAccessor->setValue($user, 'roles', ['ROLE_EDITOR', 'ROLE_REVIEWER']);
// Uses grant() and revoke() to update the rolesPropertyInfo can also infer the collection element type from the adder's argument. In this example, the grant(string $role) signature supplies the string type for the roles collection. That information can help framework features that inspect or populate objects without hard-coding each model's method names.

Serializer uses the same mapping
Serialization had a related blind spot. When deciding which property a public method represents, the serializer relied on familiar prefixes. A method named displayName() doesn't follow that pattern, so its connection to the name property wasn't apparent.
Symfony 8.2 changes the serializer's lookup: it asks PropertyInfo which property belongs to each method. As a result, a WithAccessors mapping can guide both directions. The serializer can produce the expected property names when encoding an object, then call the mapped setter and adder methods when rebuilding one from data. For the example above, the JSON representation can contain name and roles, while deserialization calls rename() and grant().
This shared metadata reduces the need to teach each framework component about a class's naming style independently. It also means that applications using forms or other features built on PropertyAccess can benefit from the mapping, provided those features rely on the PropertyInfo metadata.
Property hooks become visible to reflection metadata
The second change concerns PHP property hooks. A property read or write can run logic, and a hooked property may be virtual, meaning it doesn't store a value in the usual way. Object mappers and hydration tools need to distinguish these cases to understand what an operation on a property will do.
In Symfony 8.2, ReflectionExtractor's read and write information reports whether a property has a hook and whether it is virtual. For example, a computed fullName property with a getter hook can be reported as hooked and virtual. An email property with a setter hook is reported as hooked for writes; its read information doesn't report a hook if it has no getter hook. An ordinary property such as firstName has no hook.
The results match PHP's ReflectionProperty::hasHook() and ReflectionProperty::isVirtual() methods. When the extractor can't determine the answer—for example, when access happens through a method—the metadata can be null. These additions describe how a property behaves; they don't change how PropertyAccess reads or writes it.
What this means for application code
The practical gain is clearest in models that deliberately use verbs to protect domain rules. Teams can keep APIs such as rename() and grant() while letting Symfony's property-based tools interact through stable property names. The attribute also puts the mapping close to the property declaration, where developers can inspect it alongside the type and related methods.
There is a migration detail to handle: every method named in the attribute must exist, and collection adder and remover declarations come as a pair. Applications that currently maintain custom extractors may be able to replace some of that mapping with attributes, but the fit depends on how those extractors handle their other metadata. For property hooks, the new reflection details give mappers better information to make their own decisions; they don't automatically change hydration behavior.
For teams adopting Symfony 8.2, the useful next step is to check models whose method names don't match conventions, then verify how forms, serialization and hydration use them. The release gives those components a clearer description of object behavior; applications still need to decide how each mapped operation should preserve their domain rules.