| 1 | <?php |
||
| 7 | trait Id |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var int |
||
| 11 | */ |
||
| 12 | private $id; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param int $id |
||
| 16 | */ |
||
| 17 | public function __construct($id) |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param int $id |
||
| 24 | * |
||
| 25 | * @return Id |
||
|
|
|||
| 26 | */ |
||
| 27 | public static function create($id){ |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return int |
||
| 33 | */ |
||
| 34 | public function getId() |
||
| 38 | |||
| 39 | } |
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.