Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Last active June 5, 2019 12:31
Show Gist options
  • Save Majkl578/99948edbb16220f44ca3bb58d57d3929 to your computer and use it in GitHub Desktop.
Save Majkl578/99948edbb16220f44ca3bb58d57d3929 to your computer and use it in GitHub Desktop.
model example for typed properties
<?php
declare(strict_types=1);
final class Foo
{
var DateTimeImmutable $createdAt {
get;
private set(DateTimeImmutable $value) : void {
assert($this->createdAt === null, "Can't change time of creation.");
$this->createdAt = $value;
};
}
var DateTimeImmutable $updatedAt {
get;
set(DateTimeImmutable $value) : void {
assert($value > $this->createdAt, "Can't be updated earlier than created, are you coming from future?");
assert($value->format('w') !== '6', "Don't work on Sundays. :)");
$this->updatedAt = $value;
};
}
public function __construct()
{
$this->createdAt = new DateTimeImmutable();
}
}
$foo = new Foo();
$foo->updatedAt = new DateTimeImmutable();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment