Download the PHP package danwe/helpers-gettersetteraccessor without Composer
On this page you can find all versions of the php package danwe/helpers-gettersetteraccessor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danwe/helpers-gettersetteraccessor
More information about danwe/helpers-gettersetteraccessor
Files in danwe/helpers-gettersetteraccessor
Package helpers-gettersetteraccessor
Short Description Helper class offering functionality to define setter and getter class methods with a minimum of expressive code.
License MIT License
Homepage https://github.com/DanweDE/php-helpers-gettersetteraccessor
Informations about the package helpers-gettersetteraccessor
php-Helpers-GetterSetterAccessor
Helper class offering functionality to define combined getter and setter class methods with a minimum of expressive code. An example for a "combined getter and setter" would be User::age( $age=null )
for getting or setting an user's age instead of having two separate methods User::getAge()
and User::setAge( $age )
.
Disclaimer
This library is in no way suggesting that combined getters and setters are superior to having separate methods. There might be use cases where each way has certain advantages and disatvantages. Choosing one might often just be a question of the developer's own taste of style.
Usage
See the following usage example:
Performance Implications
You should be aware that GetterSetterAccessor
is using PHP's reflection facilities internally
to set/get property values.
This package contains a benchmark to test a class using this library versus a simple means implementation. It can be run via:
Example output:
The benchmark result indicated that using GetterSetterAccessor
is three to four times slower than
using hard coded methods doing the same thing (this might vary depending on the PHP version and
the system). If a method is expected to be called several thousand times than this could become an
actual concern.
TODOs
- Allow to set more than one type for
ofType
. - Allow setting
null
values by defining another value as the one triggering the getter.