1 | <?php |
||
14 | class Module |
||
15 | { |
||
16 | /** @var string Can be used to override the automatically generated value */ |
||
17 | public static $name; |
||
18 | |||
19 | /** @var string Can be used to override the automatically generated value */ |
||
20 | public static $namespace; |
||
21 | |||
22 | /** |
||
23 | * @var array Requirements that need to be in place for this module |
||
24 | */ |
||
25 | protected $require = array(); |
||
26 | |||
27 | /** |
||
28 | * @var array Salts that this module provides |
||
29 | */ |
||
30 | protected $provide = array(); |
||
31 | |||
32 | /** |
||
33 | * @var int bitmask of Salts passed to the registry |
||
34 | */ |
||
35 | public $registry = 0; |
||
36 | |||
37 | /** |
||
38 | * @param string $name |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function register($name) |
||
56 | |||
57 | /** |
||
58 | * Make sure all required modules are loaded |
||
59 | * |
||
60 | * @return void |
||
61 | */ |
||
62 | private function ensureRequires() |
||
72 | |||
73 | /** |
||
74 | * Register all salts the module provides |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | private function registerProvides() |
||
90 | |||
91 | /** |
||
92 | * Register a salt that this module provides |
||
93 | * |
||
94 | * @param $type |
||
95 | * @param $salt |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | private function registerProvide($type, $salt) |
||
105 | |||
106 | /** |
||
107 | * Check whether the module contains a salt |
||
108 | * |
||
109 | * @param $bit |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function has($bit) |
||
117 | |||
118 | /** |
||
119 | * Create a new provider instance |
||
120 | * |
||
121 | * @param string $type |
||
122 | * @param string $caller |
||
123 | * |
||
124 | * @return Provider |
||
125 | */ |
||
126 | public function provider($caller, $type) |
||
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * |
||
144 | * @return false|Provider |
||
145 | */ |
||
146 | private function makeProvider($type) |
||
152 | |||
153 | /** |
||
154 | * Check whether this module provides a salt |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function doesProvide($salt) |
||
162 | |||
163 | /** |
||
164 | * Return the master context for this module |
||
165 | * |
||
166 | * @return null|string |
||
167 | */ |
||
168 | public function masterContext() |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public static function getName() |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | public static function getNamespace() |
||
204 | } |
||
205 |
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.