Last active
June 5, 2019 12:31
-
-
Save Majkl578/99948edbb16220f44ca3bb58d57d3929 to your computer and use it in GitHub Desktop.
model example for typed properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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