Skip to content
Snippets Groups Projects
Commit f6ad272d authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

documentation

parent 5c363bda
Branches
Tags
1 merge request!1132Hotline#29412 images des articles dans le resultat de recherche
# 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
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment