PHP Data Object
PHP Light DataObject with minimum implementation.
From the command line run:
composer require saedyousef/dataobject
With the package now installed, you can implement the main Interface DataObject
and by using the trait DataObjectTrait
that have all the methods implemented for you.
Here is an example of a class PostDataObject
that implements the DataObject
interface :
use SY\DataObject\Contracts\DataObject;
use SY\DataObject\Support\DataObjectTrait;
use SY\DataObject\Support\ObjectReadAccess;
use SY\DataObject\Support\ObjectWriteAccess;
/**
* @property int|null id
* @property string title
* @property string body
*/
class PostDataObject implements DataObject
{
use DataObjectTrait;
use ObjectReadAccess;
use ObjectWriteAccess; // If you need to write object properties.
public function __construct(array $properties = [])
{
$this->_properties = [
'id' => null,
'title' => '',
'body' => ''
];
$this->hydrate($properties);
}
/**
* @return int|null
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
}