PHP library that lets you define strict, validated class variables (with change tracking and serialization helpers) in one place and reuse them across your own domain classes.
composer require imponeer/properties
Quick start:
- Extend
Imponeer\Properties\PropertiesSupport(or implementPropertiesInterfaceyourself). - Define each property in the constructor with
initVar()using aDataTypeenum (or the legacyDTYPE_*constants available onPropertiesInterface). - Use the generated magic accessors or helper methods like
toArray()/assignVars()as you would with normal public properties.
use DateTimeImmutable;
use Imponeer\Properties\Enum\DataType;
use Imponeer\Properties\PropertiesSupport;
final class Article extends PropertiesSupport
{
public function __construct()
{
$this->initVar('id', DataType::INTEGER, null, true);
$this->initVar('title', DataType::STRING, 'Untitled', true, ['maxlength' => 150]);
$this->initVar('published_at', DataType::DATETIME, null, false);
}
}
$article = new Article();
$article->title = 'Hello world';
$article->published_at = new DateTimeImmutable();
var_dump($article->toArray()); // Strictly typed values, ready to persist or renderIf you want to add some functionality or fix bugs, you can fork, change and create pull request. If you are new to Git or GitHub, start with the GitHub Skills “Introduction to GitHub” course.
If you found any bug or have some questions, use issues tab and write there your questions.