1 | <?php |
||
10 | class BaseRepository implements RepositoryContract |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The repository model. |
||
15 | * @var Model |
||
16 | */ |
||
17 | protected $model; |
||
18 | |||
19 | /** |
||
20 | * @param Model $model |
||
21 | * @return object |
||
22 | */ |
||
23 | public function setModel(Model $model) |
||
28 | |||
29 | /** |
||
30 | * @return Model |
||
31 | */ |
||
32 | public function getModel() |
||
36 | |||
37 | /** |
||
38 | * @return Collection |
||
39 | */ |
||
40 | public function findAll() |
||
44 | |||
45 | /** |
||
46 | * @param array $data |
||
47 | * @return Collection |
||
48 | * @throws RepositoryException |
||
49 | */ |
||
50 | public function createNew(array $data) |
||
58 | |||
59 | /** |
||
60 | * @param $itemId |
||
61 | * @throws RepositoryException |
||
62 | * @return Collection |
||
63 | */ |
||
64 | public function findItemById($itemId) |
||
72 | |||
73 | /** |
||
74 | * @param $itemId |
||
75 | * @param array $data |
||
76 | * @throws RepositoryException |
||
77 | * @return mixed |
||
78 | */ |
||
79 | public function update($itemId, array $data) |
||
91 | |||
92 | /** |
||
93 | * @param $itemId |
||
94 | * @return mixed |
||
95 | * @throws RepositoryException |
||
96 | */ |
||
97 | public function delete($itemId) |
||
105 | } |
||
106 |
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.