symfony/property-access
Symfony PropertyAccess lets you read and write values on objects and arrays using a simple property-path string syntax. Supports nested properties, indexed access, getters/setters, and safe navigation for flexible data mapping and forms.
array_get(), data_get()) while providing advanced features for deeply nested or complex object graphs (e.g., DTOs, API payloads, or dynamic configurations).$obj->getNested()->value()) in favor of string-based paths ('nested.value'). This is particularly valuable in:
symfony/serializer, symfony/form, or symfony/http-foundation), this package enables unified data access patterns across components, reducing duplication. For example:
PropertyAccess to extract data for Symfony’s Serializer or PropertyInfo.PropertyAccessor instance (singleton pattern).void or conflicting with properties) may require custom logic. Example:
// May fail if `getName()` returns void but `name` is a property.
$accessor->getValue($obj, 'name');
Mitigation: Test with domain-specific objects or extend the PropertyAccessBuilder for custom rules.user->address->user) may cause infinite loops. The component includes safeguards but should be validated in complex data structures.^6.4 (last LTS branch) or ^7.4 (backward-compatible). Check Laravel’s PHP version support policy.relations, appends, or accessors) may require custom path handlers or pre-processing.PropertyAccess vs. Symfony\Component\PropertyAccess\PropertyAccess).PropertyAccessor be cached as a singleton to avoid repeated instantiation?null, throw UndefinedPropertyException, or use a fallback).Arrayable, Jsonable, or custom mappers)?PropertyAccessBuilder)?collect(), array_get(), Eloquent accessors). Use it where it adds value (e.g., deep nesting) and avoid where Laravel’s solutions suffice (e.g., shallow access).PropertyAccessor as a singleton in Laravel’s container for global reuse:
// config/app.php
'bindings' => [
Symfony\Component\PropertyAccess\PropertyAccess::class => fn() => \Symfony\Component\PropertyAccess\PropertyAccess::createPropertyAccessor(),
];
Then inject it into services:
use Symfony\Component\PropertyAccess\PropertyAccessInterface;
class ApiRequestMapper
{
public function __construct(private PropertyAccessInterface $accessor) {}
}
$user = new User();
$accessor->setValue($user, 'profile.address.city', 'Paris');
PropertyAccess to map submitted data to deeply nested objects.Serializer component (e.g., in API responses).symfony/property-info for advanced type-aware property access.^8.0 for latest features (e.g., PHP 8.4 support).^6.4 or ^7.4 (last LTS branches). Note: ^6.4 lacks some PHP 8+ optimizations.// Before
$theme = $request->input('user.profile.settings.theme');
// After
$accessor->getValue($request->all(), 'user.profile.settings.theme');
PropertyAccessBuilder for domain-specific path resolution (e.g., handling Laravel’s appends or accessors).$builder = PropertyAccess::createPropertyAccessorBuilder();
$builder->addGetterAccessor('appends', fn($obj, $property) => $obj->{$property} ?? null);
$accessor = $builder->getPropertyAccessor();
$model->relation) may not work via PropertyAccess. Use serialized attributes or custom path handlers.Collection class, but nested traversal may require flattenHow can I help you explore Laravel packages today?