| 1 | <?php |
||
| 12 | abstract class AbstractAdapter implements Adapter |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Raw branches if needed |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | private $rawData; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Builded tree |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | private $tree; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Sets the raw branches |
||
| 28 | * @param array $data |
||
| 29 | * @return Adapter |
||
| 30 | */ |
||
| 31 | public function setRawData(array $data) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Returnes the raw data |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | protected function getRawData() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets the tree data |
||
| 48 | * @param array $data |
||
| 49 | * @return Adapter |
||
| 50 | */ |
||
| 51 | public function setTree(array $data) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Get the tree array |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | protected function getTree() |
||
| 65 | } |
||
| 66 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.