1 | <?php |
||
45 | class Report implements IDeserializable, JsonSerializable { |
||
46 | |||
47 | |||
48 | use TArrayTools; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $source = ''; |
||
52 | |||
53 | /** @var Circle[] */ |
||
54 | private $circles = []; |
||
55 | |||
56 | /** @var array */ |
||
57 | private $obfuscated = []; |
||
58 | |||
59 | |||
60 | /** |
||
61 | * Report constructor. |
||
62 | */ |
||
63 | public function __construct() { |
||
65 | |||
66 | |||
67 | /** |
||
68 | * @param string $source |
||
69 | * |
||
70 | * @return Report |
||
71 | */ |
||
72 | public function setSource(string $source): self { |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getSource(): string { |
||
84 | |||
85 | |||
86 | /** |
||
87 | * @param Circle[] $circles |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setCircles(array $circles): self { |
||
96 | |||
97 | /** |
||
98 | * @return Circle[] |
||
99 | */ |
||
100 | public function getCircles(): array { |
||
103 | |||
104 | |||
105 | /** |
||
106 | * @param array $obfuscated |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setObfuscated(array $obfuscated): self { |
||
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | public function getObfuscated(): array { |
||
122 | |||
123 | |||
124 | /** |
||
125 | * @param array $data |
||
126 | * |
||
127 | * @return IDeserializable |
||
128 | */ |
||
129 | public function import(array $data): IDeserializable { |
||
136 | |||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | function jsonSerialize(): array { |
||
148 | |||
149 | } |
||
150 | |||
151 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.