1. Go to this page and download the library: Download hsldymq/velcro library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hsldymq / velcro example snippets
useArchman\Velcro\Converters\DateTimeImmutableConverter;
useArchman\Velcro\DataModel;
useArchman\Velcro\Field;
useArchman\Velcro\RO;
classFooextendsDataModel{
#[Field('f1')]public int $p1;
#[Field('f2'), DateTimeImmutableConverter(DateTimeImmutableConverter::ISO_8601)]public DateTimeImmutable $p2;
#[Field('f3'), RO]public string $p3;
#[Field('f4')]public readonly string $p4;
}
$foo = new Foo([
'f1' => 123,
'f2' => '2021-01-01T00:00:00',
'f3' => 'value for readonly field',
'f4' => 'value for PHP 8.1 readonly field'
]);
assert($foo->p1 === 123);
assert($foo->p2->format('Y-m-d H:i:s') === '2021-01-01 00:00:00');
assert($foo->p3 === 'value for readonly field');
assert($foo->p4 === 'value for PHP 8.1 readonly field');
$foo->p3 = 'new value'; // It throws an exception.
useArchman\Velcro\DataModel;
useArchman\Velcro\Field;
classFooextendsDataModel{
#[Field('field')]public int $bar;
}
$foo = new Foo(['field' => 1]);
doSomething($foo->bar);
classFoo{
public int $bar;
publicfunction__construct(array $data){
if (array_key_exists('field', $data)) {
$this->bar = $data['field'];
}
}
}