Wallogit.com
2017 © Pedro Peláez
A very flexible data-structure (inspired by Ruby's `ostruct` library)
A very flexible data-structure (inspired by Ruby's ostruct library), (*2)
This library requires PHP 5.4+, (*3)
You can easily install Open_Struct with the following command:, (*4)
composer [global] require 'jzaleski/php-open_struct=*'
Creating a new [empty] instance:, (*5)
$struct = new Open_Struct;
Creating a new instance from an array of attributes:, (*6)
$struct = new Open_Struct(['foo' => 1, 'bar' => ['biz' => 2]]);
Dereferencing a top-level value:, (*7)
$value = $struct->foo;
Getting a top-level value by index/key:, (*8)
$value = $struct['foo'];
Dereferencing a nested value:, (*9)
$nested_value = $struct->bar->biz;
Getting a nested value by index/key:, (*10)
$nested_value = $struct['bar']['biz'];
Setting a top-level value:, (*11)
$struct->foo = 2;
Setting a top-level value by index/key:, (*12)
$struct['foo'] = 2;
Setting a nested value:, (*13)
$struct->bar->biz = 3;
Setting a nested value by index/key:, (*14)
$struct['bar']['biz'] = 3;
Unsetting a top-level value:, (*15)
unset($struct->foo);
Unsetting a top-level value by index/key:, (*16)
unset($struct['foo']);
Checking for the existence of a key:, (*17)
isset($struct->foo);
Checking for the existence of a key by index/key:, (*18)
isset($struct['foo']);
Setting a callback value (this is useful for scenarios where you want to derive or lazy-load a property):, (*19)
$dao = new Data_Access_Object;
$struct = new Open_Struct(['something' => function() use ($dao) { return $dao->get_something(); }]);
$struct->something;
The dirty method will return false until initialization (the constructor) is complete:, (*20)
$struct = new Open_Struct(['foo' => 1]); $struct->dirty(); // returns: `false`
The dirty method will return true, after initialization (the constructor), when a value is set:, (*21)
$struct = new Open_Struct; $struct->foo = 1; $struct->dirty(); // returns: `true`
The dirty method will return false, after initialization (the constructor), when a value is set back to its original state:, (*22)
$struct = new Open_Struct(['foo' => 1]); $struct->foo = 2; $struct->dirty(); // returns: `true` $struct->foo = 1; $struct->dirty(); // returns: `false`
Getting the array of attributes:, (*23)
$struct = new Open_Struct(['foo' => 1]); $struct->foo = 2; $struct->bar = 3; $struct->attributes(); // returns: `['foo' => 2, 'bar' => 3]`
Getting the array of changed attributes:, (*24)
$struct = new Open_Struct(['foo' => 1]); $struct->bar = 2; $struct->changed_attributes(); // returns: `['bar' => 2]`
The changed_attributes method will return an empty array. after initializaiton (the constructor), when a value is set back to its original state:, (*25)
$struct = new Open_Struct(['foo' => 1]); $struct->foo = 2; $struct->changed_attributes(); // returns: `['foo' => 2]` $struct->foo = 1; $struct->changed_attributes(); // returns: `[]`
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)