| 1 | <?php |
||
| 12 | trait PartialTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | private $autoload = true; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Partial selections will not be autoloaded. |
||
| 21 | * |
||
| 22 | * Example: |
||
| 23 | * |
||
| 24 | * $post = $this->findPost(); //no comments |
||
| 25 | * $post->comments->partial(true); |
||
| 26 | * $post->comments->add(new Comment()); |
||
| 27 | * assert($post->comments->count() == 1); //no other comments to be loaded |
||
| 28 | * |
||
| 29 | * $post->comments->add($comment); |
||
| 30 | * |
||
| 31 | * @param bool $partial |
||
| 32 | * |
||
| 33 | * @return self|$this |
||
|
|
|||
| 34 | */ |
||
| 35 | public function partial(bool $partial = true) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | public function isPartial(): bool |
||
| 49 | } |
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.