| 1 | <?php declare(strict_types=1); |
||
| 13 | class MediaThumbnail implements MediaThumbnailInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $url; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | protected $width; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | protected $height; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var \DateTime |
||
| 32 | */ |
||
| 33 | protected $time; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | 3 | public function getUrl() : ? string |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $url |
||
| 46 | * @return MediaThumbnailInterface |
||
| 47 | */ |
||
| 48 | 3 | public function setUrl(?string $url) : MediaThumbnailInterface |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | 2 | public function getWidth() : ?int |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param int $width |
||
| 65 | * @return MediaThumbnailInterface |
||
| 66 | */ |
||
| 67 | 3 | public function setWidth(?int $width) : MediaThumbnailInterface |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @return int |
||
| 76 | */ |
||
| 77 | 2 | public function getHeight() : ?int |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param int $height |
||
| 84 | * @return MediaThumbnailInterface |
||
| 85 | */ |
||
| 86 | 3 | public function setHeight(?int $height) : MediaThumbnailInterface |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @return \DateTime |
||
| 95 | */ |
||
| 96 | 2 | public function getTime() : ? \DateTime |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param \DateTime $time |
||
| 103 | * @return MediaThumbnailInterface |
||
| 104 | */ |
||
| 105 | 1 | public function setTime(? \DateTime $time) : MediaThumbnailInterface |
|
| 111 | } |
||
| 112 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.