ObjectAccess
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
Get a property of a given object or array.
Gets a property of a given object or array.
No description
Gets a property path from a given object or array.
Set a property for a given object.
Returns an array of properties which can be get with the getProperty() method.
Returns an array of properties which can be set with the setProperty() method.
Tells if the value of the specified property can be set by this Object Accessor.
Tells if the value of the specified property can be retrieved by this Object Accessor.
Get all properties (names and their current values) of the current $object that are accessible through this class.
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
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.
static protected void
initializePropertyGetterCache(string $cacheIdentifier, mixed $subject, string $propertyName)
No description
static mixed
getPropertyPath(mixed $subject, string $propertyPath)
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.
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
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.
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.
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".