abstract class ObjectAccess (View source)

Provides methods to call appropriate getter/setter on an object given the property name. It does this following these rules:

  • if the target object is an instance of ArrayAccess, it gets/sets the property
  • if public getter/setter method exists, call it.
  • if public property exists, return/set the value of it.
  • else, throw exception

Some methods support arrays as well, most notably getProperty() and getPropertyPath().

Constants

ACCESS_GET

ACCESS_SET

ACCESS_PUBLIC

Properties

static protected array $gettablePropertyNamesCache

Internal RuntimeCache for getGettablePropertyNames()

static protected array $propertyGetterCache

Internal RuntimeCache for getPropertyInternal()

Methods

static mixed
getProperty(mixed $subject, string|int $propertyName, bool $forceDirectAccess = false)

Get a property of a given object or array.

static mixed
getPropertyInternal(mixed $subject, string $propertyName, bool $forceDirectAccess, bool $propertyExists)

Gets a property of a given object or array.

static void
initializePropertyGetterCache(string $cacheIdentifier, mixed $subject, string $propertyName)

No description

static mixed
getPropertyPath(mixed $subject, string $propertyPath = null)

Gets a property path from a given object or array.

static bool
setProperty(mixed $subject, string|int $propertyName, mixed $propertyValue, bool $forceDirectAccess = false)

Set a property for a given object.

static array
getGettablePropertyNames(object $object)

Returns an array of properties which can be get with the getProperty() method.

static array
getSettablePropertyNames(object $object)

Returns an array of properties which can be set with the setProperty() method.

static bool
isPropertySettable(object $object, string $propertyName)

Tells if the value of the specified property can be set by this Object Accessor.

static bool
isPropertyGettable(object $object, string $propertyName)

Tells if the value of the specified property can be retrieved by this Object Accessor.

static array
getGettableProperties(object $object)

Get all properties (names and their current values) of the current $object that are accessible through this class.

static string
buildSetterMethodName(string $propertyName)

Build the setter method name for a given property by capitalizing the first letter of the property, and prepending it with "set".

Details

static mixed getProperty(mixed $subject, string|int $propertyName, bool $forceDirectAccess = false)

Get a property of a given object or array.

Tries to get the property the following ways:

  • if the target is an array, and has this property, we return it.
  • if super cow powers should be used, fetch value through reflection
  • if public getter method exists, call it.
  • if the target object is an instance of ArrayAccess, it gets the property on it if it exists.
  • if public property exists, return the value of it.
  • else, throw exception

Parameters

mixed $subject

Object or array to get the property from

string|int $propertyName

Name or index of the property to retrieve

bool $forceDirectAccess

Directly access property using reflection(!)

Return Value

mixed

Value of the property

Exceptions

InvalidArgumentException
PropertyNotAccessibleException

static protected mixed getPropertyInternal(mixed $subject, string $propertyName, bool $forceDirectAccess, bool $propertyExists)

Gets a property of a given object or array.

This is an internal method that does only limited type checking for performance reasons.

If you can't make sure that $subject is either of type array or object and $propertyName of type string you should use getProperty() instead.

Parameters

mixed $subject

Object or array to get the property from

string $propertyName

name of the property to retrieve

bool $forceDirectAccess

directly access property using reflection(!)

bool $propertyExists

(by reference) will be set to true if the specified property exists and is gettable

Return Value

mixed

Value of the property

Exceptions

PropertyNotAccessibleException

See also

getProperty()

static protected void initializePropertyGetterCache(string $cacheIdentifier, mixed $subject, string $propertyName)

No description

Parameters

string $cacheIdentifier
mixed $subject
string $propertyName

Return Value

void

static mixed getPropertyPath(mixed $subject, string $propertyPath = null)

Gets a property path from a given object or array.

If propertyPath is "bla.blubb", then we first call getProperty($object, 'bla'), and on the resulting object we call getProperty(..., 'blubb').

For arrays the keys are checked likewise.

Parameters

mixed $subject

An object or array

string $propertyPath

Return Value

mixed

Value of the property

static bool setProperty(mixed $subject, string|int $propertyName, mixed $propertyValue, bool $forceDirectAccess = false)

Set a property for a given object.

Tries to set the property the following ways:

  • if target is an array, set value
  • if super cow powers should be used, set value through reflection
  • if public setter method exists, call it.
  • if public property exists, set it directly.
  • if the target object is an instance of ArrayAccess, it sets the property on it without checking if it existed.
  • else, return false

Parameters

mixed $subject

The target object or array

string|int $propertyName

Name or index of the property to set

mixed $propertyValue

Value of the property

bool $forceDirectAccess

directly access property using reflection(!)

Return Value

bool

true if the property could be set, false otherwise

Exceptions

InvalidArgumentException

static array getGettablePropertyNames(object $object)

Returns an array of properties which can be get with the getProperty() method.

Includes the following properties:

  • which can be get through a public getter method.
  • public properties which can be directly get.

Parameters

object $object

Object to receive property names for

Return Value

array

Array of all gettable property names

Exceptions

InvalidArgumentException

static array getSettablePropertyNames(object $object)

Returns an array of properties which can be set with the setProperty() method.

Includes the following properties:

  • which can be set through a public setter method.
  • public properties which can be directly set.

Parameters

object $object

Object to receive property names for

Return Value

array

Array of all settable property names

Exceptions

InvalidArgumentException

static bool isPropertySettable(object $object, string $propertyName)

Tells if the value of the specified property can be set by this Object Accessor.

Parameters

object $object

Object containing the property

string $propertyName

Name of the property to check

Return Value

bool

Exceptions

InvalidArgumentException

static bool isPropertyGettable(object $object, string $propertyName)

Tells if the value of the specified property can be retrieved by this Object Accessor.

Parameters

object $object

Object containing the property

string $propertyName

Name of the property to check

Return Value

bool

Exceptions

InvalidArgumentException

static array getGettableProperties(object $object)

Get all properties (names and their current values) of the current $object that are accessible through this class.

What to do with ArrayAccess

Parameters

object $object

Object to get all properties from.

Return Value

array

Associative array of all properties.

Exceptions

InvalidArgumentException

static string buildSetterMethodName(string $propertyName)

Build the setter method name for a given property by capitalizing the first letter of the property, and prepending it with "set".

Parameters

string $propertyName

Name of the property

Return Value

string

Name of the setter method name