1 | <?php |
||
45 | class ItemList implements ItemListInterface |
||
46 | { |
||
47 | /** |
||
48 | * Items |
||
49 | * |
||
50 | * @var ItemInterface[] |
||
51 | */ |
||
52 | protected $items; |
||
53 | |||
54 | /** |
||
55 | * Internal pointer |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $pointer; |
||
60 | |||
61 | /** |
||
62 | * ItemList constructor |
||
63 | * |
||
64 | * @param ItemInterface[] $items Items |
||
65 | */ |
||
66 | 1 | public function __construct(array $items = []) |
|
71 | |||
72 | /** |
||
73 | * Return the current item |
||
74 | * |
||
75 | * @return ItemInterface Item |
||
76 | */ |
||
77 | public function current() |
||
81 | |||
82 | /** |
||
83 | * Move forward to next element |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | public function next() |
||
91 | |||
92 | /** |
||
93 | * Return the position of the current element |
||
94 | * |
||
95 | * @return mixed Position of the current element |
||
96 | */ |
||
97 | public function key() |
||
101 | |||
102 | /** |
||
103 | * Checks if current position is valid |
||
104 | * |
||
105 | * @return boolean The current position is valid |
||
106 | */ |
||
107 | public function valid() |
||
111 | |||
112 | /** |
||
113 | * Rewind the item list to the first element |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function rewind() |
||
121 | |||
122 | /** |
||
123 | * Return an object representation of the item list |
||
124 | * |
||
125 | * @return \stdClass Micro information items |
||
126 | * @api |
||
127 | */ |
||
128 | public function toObject() |
||
132 | |||
133 | /** |
||
134 | * Return a JSON representation of the item list |
||
135 | * |
||
136 | * @return string Micro information items |
||
137 | * @api |
||
138 | */ |
||
139 | public function toJson() |
||
143 | |||
144 | /** |
||
145 | * Return the first item, optionally of particular types |
||
146 | * |
||
147 | * @param array ...$types Item types |
||
148 | * @return ItemInterface Item |
||
149 | * @api |
||
150 | */ |
||
151 | public function item(...$types) |
||
155 | |||
156 | /** |
||
157 | * Filter the items by item type(s) |
||
158 | * |
||
159 | * @param array ...$types Item types |
||
160 | * @return ItemListInterface Items matching the requested types |
||
161 | * @api |
||
162 | */ |
||
163 | public function filter(...$types) |
||
167 | |||
168 | /** |
||
169 | * Return all items as an array, optionally filtered by item type(s) |
||
170 | * |
||
171 | * @param array ...$types Item types |
||
172 | * @return ItemInterface[] Items matching the requested types |
||
173 | * @api |
||
174 | */ |
||
175 | public function items(...$types) |
||
189 | } |
||
190 |
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.