| 1 | <?php |
||
| 17 | trait WithBodyTrait |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Request body |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $body = false; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Set the body for the current request |
||
| 27 | * @param string $body |
||
| 28 | * @return WithBodyTrait |
||
|
|
|||
| 29 | */ |
||
| 30 | 13 | public function setBody($body) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Return the body property |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | 8 | public function getBody() |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Retrieve the body length |
||
| 47 | * @return integer |
||
| 48 | */ |
||
| 49 | 1 | public function getBodyLength() |
|
| 53 | } |
||
| 54 |
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.