1 | <?php |
||
7 | trait DateTrait |
||
8 | { |
||
9 | /** |
||
10 | * @var \DateTimeImmutable|null |
||
11 | */ |
||
12 | private $value; |
||
13 | |||
14 | private function __construct(\DateTimeImmutable $value = null) |
||
18 | |||
19 | /** |
||
20 | * @param \DateTimeImmutable $value |
||
21 | * |
||
22 | * @return DateTrait|static |
||
|
|||
23 | */ |
||
24 | public static function create(\DateTimeImmutable $value): self |
||
28 | |||
29 | /** |
||
30 | * @return DateTrait|static |
||
31 | */ |
||
32 | public static function zero(): self |
||
36 | |||
37 | /** |
||
38 | * @param string|null $date |
||
39 | * |
||
40 | * @return DateTrait|static |
||
41 | */ |
||
42 | public static function fromString(string $date = null): self |
||
50 | |||
51 | public function asString() |
||
59 | |||
60 | } |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.