1 | <?php |
||
29 | class Pool implements PoolInterface, OutputableInterface |
||
30 | { |
||
31 | /** |
||
32 | * Default amount of workers to spawn |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | const DEFAULT_WORKER_COUNT = 1; |
||
37 | |||
38 | /** |
||
39 | * Immutable. Number of workers this pool maintain |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $numberOfWorkers; |
||
44 | |||
45 | /** |
||
46 | * Pool of worker objects |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $workers = array(); |
||
51 | |||
52 | /** |
||
53 | * @var WorkerInterface |
||
54 | */ |
||
55 | protected $workerInstance; |
||
56 | |||
57 | /** |
||
58 | * @var OutputInterface |
||
59 | */ |
||
60 | protected $output; |
||
61 | |||
62 | /** |
||
63 | * Timestamp of when the pool first started |
||
64 | * |
||
65 | * @var |
||
66 | */ |
||
67 | protected $started; |
||
68 | |||
69 | /** |
||
70 | * Pool constructor. |
||
71 | * |
||
72 | * @param null $numberOfWorkers |
||
73 | */ |
||
74 | 9 | public function __construct($numberOfWorkers = null) |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 8 | public function add(WorkerInterface $worker) |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 8 | public function setWorkerInstance(WorkerInterface $worker) |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 1 | public function rebuild() |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 5 | public function spawn() |
|
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 2 | public function kill($pid = null) |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 1 | public function killAll() |
|
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | 3 | public function boot() |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function ping() |
||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 7 | public function getWorkers() |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | 9 | public function setOutput(OutputInterface $output) |
|
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | 6 | public function getOutput() |
|
246 | } |
||
247 |
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.