1 | <?php |
||
12 | abstract class AbstractBranch |
||
13 | { |
||
14 | /** |
||
15 | * Container for current branch children |
||
16 | * @var array |
||
17 | */ |
||
18 | private $children = array(); |
||
19 | |||
20 | /** |
||
21 | * Reference to the current branch parent |
||
22 | * @var Branch |
||
23 | */ |
||
24 | private $parent; |
||
25 | |||
26 | /** |
||
27 | * The depth of the item |
||
28 | * @var int |
||
29 | */ |
||
30 | private $depth; |
||
31 | |||
32 | /** |
||
33 | * The left number of the branch |
||
34 | * @var int |
||
35 | */ |
||
36 | private $left; |
||
37 | |||
38 | /** |
||
39 | * The right number of the branch |
||
40 | * @var int |
||
41 | */ |
||
42 | private $right; |
||
43 | |||
44 | /** |
||
45 | * Establish if the item is a leaf |
||
46 | * @var boolean |
||
47 | */ |
||
48 | private $isLeaf; |
||
49 | |||
50 | /** |
||
51 | * Set the Parent of the current branch |
||
52 | * @param Branch $item |
||
53 | * @return Branch |
||
54 | */ |
||
55 | public function setParent(Branch $item) |
||
60 | |||
61 | /** |
||
62 | * Verify if the current branch has a parent |
||
63 | * @return boolean |
||
64 | */ |
||
65 | public function hasParent() |
||
72 | |||
73 | /** |
||
74 | * Get the current branch's parent |
||
75 | * @return Branch |
||
76 | * @throws \Exception |
||
77 | */ |
||
78 | public function getParent() |
||
85 | |||
86 | /** |
||
87 | * Get the root parent of the current branch |
||
88 | * @return Branch |
||
89 | */ |
||
90 | public function getRootParent() |
||
101 | |||
102 | /** |
||
103 | * Adds a new branch child |
||
104 | * @param Branch $item |
||
105 | */ |
||
106 | public function addChild(Branch $item) |
||
110 | |||
111 | /** |
||
112 | * Verify if the current branch has any children |
||
113 | * @return boolean |
||
114 | */ |
||
115 | public function hasChildren() |
||
122 | |||
123 | /** |
||
124 | * Return the current branch children |
||
125 | * @return array - array(Branch, Branch) |
||
126 | * @throws \Exception |
||
127 | */ |
||
128 | public function getChildren() |
||
135 | |||
136 | /** |
||
137 | * Set the branch depth |
||
138 | * @param int $depth |
||
139 | * @return AbstractBranch |
||
140 | */ |
||
141 | public function setDepth($depth) |
||
146 | |||
147 | /** |
||
148 | * Get the branch depth |
||
149 | * @return int |
||
150 | */ |
||
151 | public function getDepth() |
||
155 | |||
156 | /** |
||
157 | * Set Tree Left value |
||
158 | * @param int $value |
||
159 | * @return AbstractBranch |
||
160 | */ |
||
161 | public function setLeft($value) |
||
166 | |||
167 | /** |
||
168 | * Get Tree Left value |
||
169 | * @return int |
||
170 | */ |
||
171 | public function getLeft() |
||
175 | |||
176 | |||
177 | /** |
||
178 | * Set Tree Right value |
||
179 | * @param int $value |
||
180 | * @return AbstractBranch |
||
181 | */ |
||
182 | public function setRight($value) |
||
187 | |||
188 | /** |
||
189 | * Get Tree Right value |
||
190 | * @return int |
||
191 | */ |
||
192 | public function getRight() |
||
196 | |||
197 | /** |
||
198 | * Set the Leaf state |
||
199 | * @param boolean $isLeaf |
||
200 | * @return AbstractBranch |
||
201 | */ |
||
202 | public function setIsLeaf($isLeaf) |
||
207 | |||
208 | /** |
||
209 | * Return the leaf state of the item |
||
210 | * @return boolean |
||
211 | */ |
||
212 | public function isLeaf() |
||
216 | |||
217 | /** |
||
218 | * Get the entire Item as array |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getAsArray() |
||
233 | } |
||
234 |
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.