1 | <?php |
||
33 | class Task implements TaskInterface |
||
34 | { |
||
35 | /** |
||
36 | * Collection |
||
37 | * |
||
38 | * @var mixed |
||
39 | * |
||
40 | * @access protected |
||
41 | */ |
||
42 | protected $collection; |
||
43 | |||
44 | /** |
||
45 | * Item tasks |
||
46 | * |
||
47 | * @var QueueInterface |
||
48 | * |
||
49 | * @access protected |
||
50 | */ |
||
51 | protected $itemTasks; |
||
52 | |||
53 | /** |
||
54 | * Collection tasks |
||
55 | * |
||
56 | * @var QueueInterface |
||
57 | * |
||
58 | * @access protected |
||
59 | */ |
||
60 | protected $collectionTasks; |
||
61 | |||
62 | /** |
||
63 | * __construct |
||
64 | * |
||
65 | * @param mixed $collection DESCRIPTION |
||
66 | * @param QueueInterface $itemTaskQueue DESCRIPTION |
||
67 | * @param QueueInterface $collectionTaskQueue DESCRIPTION |
||
68 | * |
||
69 | * @access public |
||
70 | */ |
||
71 | 3 | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * Add an item task |
||
83 | * |
||
84 | * @param mixed $task task or task spec |
||
85 | * @param int $priority priority of task |
||
86 | * |
||
87 | * @return $this |
||
88 | * |
||
89 | * @access public |
||
90 | */ |
||
91 | 3 | public function each($task, $priority = 1000) |
|
96 | |||
97 | /** |
||
98 | * Add Item Tasks |
||
99 | * |
||
100 | * @param mixed $tasks tasks to add |
||
101 | * @param int $priority task priority |
||
102 | * |
||
103 | * @return $this |
||
104 | * |
||
105 | * @access public |
||
106 | */ |
||
107 | 1 | public function addItemTasks($tasks, $priority = 1000) |
|
114 | |||
115 | /** |
||
116 | * Set Item Tasks |
||
117 | * |
||
118 | * @param mixed $tasks Tasks to set |
||
119 | * @param int $priority Task priority |
||
120 | * |
||
121 | * @return $this |
||
122 | * |
||
123 | * @access public |
||
124 | */ |
||
125 | 1 | public function setItemTasks($tasks, $priority = 1000) |
|
130 | |||
131 | /** |
||
132 | * Add a collection task |
||
133 | * |
||
134 | * @param mixed $task task |
||
135 | * @param int $priority order priority |
||
136 | * |
||
137 | * @return $this |
||
138 | * |
||
139 | * @access public |
||
140 | */ |
||
141 | 3 | public function all($task, $priority = 1000) |
|
146 | |||
147 | /** |
||
148 | * Add Collection Tasks |
||
149 | * |
||
150 | * @param mixed $tasks tasks to add |
||
151 | * @param int $priority task priority |
||
152 | * |
||
153 | * @return $this |
||
154 | * |
||
155 | * @access public |
||
156 | */ |
||
157 | 1 | public function addCollectionTasks($tasks, $priority = 1000) |
|
164 | |||
165 | /** |
||
166 | * Set Collection Tasks |
||
167 | * |
||
168 | * @param mixed $tasks tasks to set |
||
169 | * @param int $priority priority of tasks |
||
170 | * |
||
171 | * @return $this |
||
172 | * |
||
173 | * @access public |
||
174 | */ |
||
175 | 1 | public function setCollectionTasks($tasks, $priority = 1000) |
|
180 | |||
181 | /** |
||
182 | * Get collection |
||
183 | * |
||
184 | * @return mixed |
||
185 | * |
||
186 | * @access public |
||
187 | */ |
||
188 | 2 | public function getCollection() |
|
192 | |||
193 | /** |
||
194 | * Get item tasks |
||
195 | * |
||
196 | * @return QueueInterface |
||
197 | * |
||
198 | * @access public |
||
199 | */ |
||
200 | 3 | public function getItemTasks() |
|
204 | |||
205 | /** |
||
206 | * Get collection tasks |
||
207 | * |
||
208 | * @return QueueInterface |
||
209 | * |
||
210 | * @access public |
||
211 | */ |
||
212 | 3 | public function getCollectionTasks() |
|
216 | } |
||
217 |
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.