Key Value Coding
PHP Library to implement key-value coding
This library defined 3 interfaces to enable key-value coding, key-value changing and key-value observing. Key-value coding is known as accessing properties, such as attributes or relationships of an object via keys.
The key-value-coding library ships with 2 traits that implement the coding and changing interfaces., (*1)
Usage
Every object that has methods named get* and set* can be used for key-value coding. The Object\CodingTrait implements the default mechanism which has the following workflow:
Getting a key:, (*2)
echo $object->userName;
will do:, (*3)
$object->__get("userName");
$object->valueForKey("userName");
$object->getDefinedKeys();
// If key does exist
$object->getUserName();
// If key does not exist
$object->valueForUndefinedKey("userName");
and the same for, (*4)
$object->userName = "tasoft";
but using the __set, setValueForKey, setUserName or setValueForUndefinedKey instead., (*5)
Key Paths
Keypaths are defined as identifier to trace objects by its relationships. For example, an address object contains an attribute named country and a user contains a relationship to an address object, you can access the country attribute by using, (*6)
echo $user->valueForKeyPath("address.country");
// or
echo $user["address.country"];