Download the PHP package revinate/php-getter-setter without Composer
On this page you can find all versions of the php package revinate/php-getter-setter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download revinate/php-getter-setter
More information about revinate/php-getter-setter
Files in revinate/php-getter-setter
Package php-getter-setter
Short Description PHP library to simplify getting and setting values in arrays and objects.
License MIT
Informations about the package php-getter-setter
phpGetterSetter
PHP Library for simplifying getting and setting values in arrays or objects
Summary
At its core, this library is composed of 3 function pairs:
get
/set
-- functions to get and set nested values in an array, object, or class given a field name using dot notationgetValue
/setValue
-- functions to get and set values in an array, object, or class.getValueByArrayPath
/setValueByArrayPath
-- used byget
andset
to access the nested values by specifying the path in an array.
Instalation
Use Composer:
Usage
To make things easier, include the follow use statement at the top of your files:
get and set
Getting a Value
Getting a Value with a default
Setting a Value
Here is an example unit test to give a bit more context.
Differences between objects and arrays
With setValue
, an object will get updated when a field value is set. But it is different with arrays because they are immutable. Only the array returned from setValue
will have the updated fields. Watch out for ArrayObjects
, they will get updated just like normal object
s.
Example Unit Test showing difference between objects and arrays:
Getting and Setting Nested Values
getPathValue
and setPathValue
provide an easy shortcut for getting and setting nested values.
Example accessing:
The notation is longer than $data->address->state
, but it will not blow up where this will: $data->address->zip
.
Support for Getters, Setters, Has'ers, and Is'ers.
These functions support getters, setters and magic methods use by many ORM systems like Doctrine.
Support for Magic methods like get and set
Example Data Class
Sample Usage:
All versions of php-getter-setter with dependencies
ext-json Version *