Basics
Create an object
Most encapsulations can be initialized with data in their constructor. The data is needed as associative array:
Setting values
Setting a single value to a field
Unsetting a value
Setting several values
You can set values of several fields at once. All keys of an associative array will overwrite or set the corresponding field:
Adding a value to an array
If a field of your encapsulation holds an array you can append an item to it:
The array in 'myList'
will now be [1,2,3,4]
If a field does not exist a new array will be created and filled with the given value.
If a field does exist and is an array the given value will be appended to it.
If a field does exist but is not an array you will get an exception.
Adding several values to an array
You also can add more than one value to an array:
The array in 'fruits'
will now be:
['apple', 'banana', 'melon', 'orange', 'grapefruit', 'kiwi']
Getting values
Get a single value
Getting several values as associative array
You can access more than one value of an encapsulation and get them as associative array by providing an array with a list of all requested fields. Each requested field will appear as key in the array result:
Getting all values as associative array
An encapsulation can be "converted" into an array by getting an associative array of all of it's values:
More
Check if a field exists
The has
-method does only check if a field is present. It does not check if it's value is empty or not.
Get an array of all fields
Check if an object has values
ArrayEncapsulations will check if fields are present regardless of wether the value is empty.
PropertyEncapsulations always have fields so they check if all fields are empty.
Iterate over all fields
You can iterate over one encapsulation object and get each field with it's value:
Cloning
An encapsulation will always be deep-cloned which means cloning an encapsulation clones it's inner objects too which allows to clone a whole encapsulation tree at once.
Last updated