From f6ad272d15c63f2f73044531a414675689afe0f2 Mon Sep 17 00:00:00 2001 From: pbarroca <pbarroca@afi-sa.fr> Date: Thu, 30 Jul 2015 15:36:46 +0200 Subject: [PATCH] documentation --- VERSIONS_DEV/Class_Entity.md | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 VERSIONS_DEV/Class_Entity.md diff --git a/VERSIONS_DEV/Class_Entity.md b/VERSIONS_DEV/Class_Entity.md new file mode 100644 index 00000000000..44f57ced888 --- /dev/null +++ b/VERSIONS_DEV/Class_Entity.md @@ -0,0 +1,48 @@ +# Class_Entity + +## Description + +In PHP we have StdClass on which we can set and get any direct property + +```php +$o = new StdClass(); +$o->title = 'my title'; +echo $o->title; // -> my title +``` + +_Class_Entity_ provides same functionality with getter and setter methods + + +```php +$o = new Class_Entity(); +$o->setTitle('my title'); +echo $o->getTitle(); // -> my title +``` + +## Implementation + +Any call to set*() will be catched to populate an internal key => value array, it will return $this to chain calls. +Any call to get*() will look for an existing key in internal array and will return its value or null if not found. + +Keys of internal array are not transformed in any way +* setMyValue will produce a 'MyValue' key +* set_my_value will produce a '_my_value' key +* setmyvalue will produce a 'myvalue' key + +You can define default values by setting + +```php +protected $_attribs = ['MyValue' => '']; +``` + +Then a getMyValue() call will return an empty string instead of null if not set. + +## Use cases + +### In tests + +When you have an API relying on getter and setter methods you can provide a _Class_Entity_ as a mock and easily verify that an attribute has been set or prepare it with attributes needed by tested code. + +### In general + +When you are modeling an object and don't want to write all those standard getters and setters just extend Class_Entity. \ No newline at end of file -- GitLab