1 | <?php |
||
19 | class Entry implements ResponseInterface, \ArrayAccess |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $data = null; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $rawData = null; |
||
30 | |||
31 | /** |
||
32 | * @var Entry |
||
33 | */ |
||
34 | protected $metadata = null; |
||
35 | |||
36 | /** |
||
37 | * Entry constructor. |
||
38 | * |
||
39 | * @param $data |
||
40 | */ |
||
41 | public function __construct($data) |
||
66 | |||
67 | /** |
||
68 | * Get the entry data |
||
69 | * |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getData() |
||
76 | |||
77 | /** |
||
78 | * Get the entry metadata |
||
79 | * |
||
80 | * @return Entry |
||
81 | */ |
||
82 | public function getMetaData() |
||
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getRawData() |
||
94 | |||
95 | public function offsetExists($offset) |
||
99 | |||
100 | public function offsetGet($offset) |
||
104 | |||
105 | public function offsetSet($offset, $value) |
||
109 | |||
110 | public function offsetUnset($offset) |
||
114 | |||
115 | public function __get($name) |
||
123 | |||
124 | public function __set($name, $value) |
||
128 | |||
129 | /** |
||
130 | * Gets the object representation of this entry |
||
131 | * |
||
132 | * @return object |
||
133 | */ |
||
134 | public function jsonSerialize() |
||
141 | } |
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.