1 | <?php |
||
51 | class Thing implements ThingInterface |
||
52 | { |
||
53 | /** |
||
54 | * Resource type |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $type; |
||
59 | /** |
||
60 | * Resource vocabulary |
||
61 | * |
||
62 | * @var VocabularyInterface |
||
63 | */ |
||
64 | protected $vocabulary; |
||
65 | /** |
||
66 | * Resource ID |
||
67 | * |
||
68 | * @var string|null |
||
69 | */ |
||
70 | protected $resourceId = null; |
||
71 | /** |
||
72 | * Child things |
||
73 | * |
||
74 | * @var ThingInterface[] |
||
75 | */ |
||
76 | protected $children = []; |
||
77 | /** |
||
78 | * Property |
||
79 | * |
||
80 | * @var array[] |
||
81 | */ |
||
82 | protected $properties = []; |
||
83 | |||
84 | /** |
||
85 | * Thing constructor |
||
86 | * |
||
87 | * @param string $type Resource type |
||
88 | * @param VocabularyInterface $vocabulary Vocabulary in use |
||
89 | * @param null|string $resourceId Resource id |
||
90 | */ |
||
91 | 8 | public function __construct($type, VocabularyInterface $vocabulary, $resourceId = null) |
|
105 | |||
106 | /** |
||
107 | * Return the resource type |
||
108 | * |
||
109 | * @return string Resource type |
||
110 | */ |
||
111 | 1 | public function getType() |
|
115 | |||
116 | /** |
||
117 | * Return the vocabulary in use |
||
118 | * |
||
119 | * @return VocabularyInterface Vocabulary |
||
120 | */ |
||
121 | 1 | public function getVocabulary() |
|
125 | |||
126 | /** |
||
127 | * Return the resource ID |
||
128 | * |
||
129 | * @return null|string Resource ID |
||
130 | */ |
||
131 | 2 | public function getResourceId() |
|
135 | |||
136 | /** |
||
137 | * Add a property value |
||
138 | * |
||
139 | * @param PropertyInterface $property Property |
||
140 | * @return Thing Self reference |
||
141 | */ |
||
142 | 1 | public function addProperty(PropertyInterface $property) |
|
154 | |||
155 | /** |
||
156 | * Return all properties |
||
157 | * |
||
158 | * @return array[] Properties |
||
159 | */ |
||
160 | 2 | public function getProperties() |
|
164 | |||
165 | /** |
||
166 | * Return the values of a single property |
||
167 | * |
||
168 | * @param string $name Property name |
||
169 | * @return array Property values |
||
170 | */ |
||
171 | 2 | public function getProperty($name) |
|
185 | |||
186 | /** |
||
187 | * Add a child |
||
188 | * |
||
189 | * @param ThingInterface $child Child |
||
190 | * @return Thing Self reference |
||
191 | */ |
||
192 | 2 | public function addChild(ThingInterface $child) |
|
197 | |||
198 | /** |
||
199 | * Return all children |
||
200 | * |
||
201 | * @return ThingInterface[] Children |
||
202 | */ |
||
203 | 3 | public function getChildren() |
|
207 | } |
||
208 |
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.