1 | <?php |
||
21 | class SelectUser extends Field |
||
22 | { |
||
23 | /** |
||
24 | * Properties to be injected as attributes. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $injectedProperties = []; |
||
29 | |||
30 | /** |
||
31 | * Prints out the current tag. |
||
32 | * |
||
33 | * @return string An input tag |
||
34 | */ |
||
35 | 1 | public function render() |
|
52 | |||
53 | /** |
||
54 | * Returns Html of selected users. |
||
55 | * |
||
56 | * @param string $id |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 1 | private function createDataList($id) |
|
72 | |||
73 | /** |
||
74 | * Returns Html of a selected user row. |
||
75 | * |
||
76 | * @param int $id |
||
77 | * @param string $name |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function formatSelected($id, $name) |
||
89 | |||
90 | /** |
||
91 | * Returns an array of selected users. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getValue() |
||
103 | } |
||
104 |
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.