PropertyAccessor

class PropertyAccessor implements PropertyAccessorInterface

Default implementation of {@link PropertyAccessorInterface}.

Constants

VALUE
REF
IS_REF_CHAINED
ACCESS_HAS_PROPERTY
ACCESS_TYPE
ACCESS_NAME
ACCESS_REF
ACCESS_ADDER
ACCESS_REMOVER
ACCESS_TYPE_METHOD
ACCESS_TYPE_PROPERTY
ACCESS_TYPE_MAGIC
ACCESS_TYPE_ADDER_AND_REMOVER
ACCESS_TYPE_NOT_FOUND
CACHE_PREFIX_READ
CACHE_PREFIX_WRITE
CACHE_PREFIX_PROPERTY_PATH

Methods

__construct(bool $magicCall = false, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null)

Should not be used by application code. Use {@link PropertyAccess::createPropertyAccessor()} instead.

mixed getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns the value at the end of the property path of the object graph.

setValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value)

Sets the value at the end of the property path of the object graph.

static handleError($type, $message, $file, $line, $context)
bool isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns whether a property path can be read from an object graph.

bool isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns whether a value can be written at a given property path.

static AdapterInterface createCache(string $namespace, int $defaultLifetime, string $version, LoggerInterface $logger = null)

Creates the APCu adapter if applicable.

Details

__construct(bool $magicCall = false, bool $throwExceptionOnInvalidIndex = false, CacheItemPoolInterface $cacheItemPool = null)

Should not be used by application code. Use {@link PropertyAccess::createPropertyAccessor()} instead.

Parameters

bool $magicCall
bool $throwExceptionOnInvalidIndex
CacheItemPoolInterface $cacheItemPool

mixed getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns the value at the end of the property path of the object graph.

Example:

use Symfony\Component\PropertyAccess\PropertyAccess;

$propertyAccessor = PropertyAccess::createPropertyAccessor();

echo $propertyAccessor->getValue($object, 'child.name);
// equals echo $object->getChild()->getName();

This method first tries to find a public getter for each property in the path. The name of the getter must be the camel-cased property name prefixed with "get", "is", or "has".

If the getter does not exist, this method tries to find a public property. The value of the property is then returned.

If none of them are found, an exception is thrown.

Parameters

object|array $objectOrArray The object or array to traverse
string|PropertyPathInterface $propertyPath The property path to read

Return Value

mixed The value at the end of the property path

Exceptions

InvalidArgumentException If the property path is invalid
AccessException If a property/index does not exist or is not public
UnexpectedTypeException If a value within the path is neither object nor array

setValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value)

Sets the value at the end of the property path of the object graph.

Example:

use Symfony\Component\PropertyAccess\PropertyAccess;

$propertyAccessor = PropertyAccess::createPropertyAccessor();

echo $propertyAccessor->setValue($object, 'child.name', 'Fabien');
// equals echo $object->getChild()->setName('Fabien');

This method first tries to find a public setter for each property in the path. The name of the setter must be the camel-cased property name prefixed with "set".

If the setter does not exist, this method tries to find a public property. The value of the property is then changed.

If neither is found, an exception is thrown.

Parameters

object|array $objectOrArray The object or array to modify
string|PropertyPathInterface $propertyPath The property path to modify
mixed $value The value to set at the end of the property path

Exceptions

InvalidArgumentException If the property path is invalid
AccessException If a property/index does not exist or is not public
UnexpectedTypeException If a value within the path is neither object nor array

static handleError($type, $message, $file, $line, $context)

Parameters

$type
$message
$file
$line
$context

bool isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns whether a property path can be read from an object graph.

Whenever this method returns true, {@link getValue()} is guaranteed not to throw an exception when called with the same arguments.

Parameters

object|array $objectOrArray The object or array to check
string|PropertyPathInterface $propertyPath The property path to check

Return Value

bool Whether the property path can be read

Exceptions

InvalidArgumentException If the property path is invalid

bool isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath)

Returns whether a value can be written at a given property path.

Whenever this method returns true, {@link setValue()} is guaranteed not to throw an exception when called with the same arguments.

Parameters

object|array $objectOrArray The object or array to check
string|PropertyPathInterface $propertyPath The property path to check

Return Value

bool Whether the value can be set

Exceptions

InvalidArgumentException If the property path is invalid

static AdapterInterface createCache(string $namespace, int $defaultLifetime, string $version, LoggerInterface $logger = null)

Creates the APCu adapter if applicable.

Parameters

string $namespace
int $defaultLifetime
string $version
LoggerInterface $logger

Return Value

AdapterInterface

Exceptions

RuntimeException When the Cache Component isn't available

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/4.0/Symfony/Component/PropertyAccess/PropertyAccessor.html