1 | <?php |
||
21 | class BaseModel implements BaseModelInterface |
||
22 | { |
||
23 | // Inject the inflector trait |
||
24 | use Inflector; |
||
25 | |||
26 | // Private variable that contains instance of database |
||
27 | protected $databaseModel; |
||
28 | |||
29 | // Class variable holding class name pluralized |
||
30 | protected $tableName; |
||
31 | |||
32 | // Properties will later contain key, value pairs from the magic setter, getter methods |
||
33 | protected $properties = []; |
||
34 | |||
35 | public function __construct() |
||
43 | |||
44 | /** |
||
45 | * The magic getter method |
||
46 | * @params key |
||
47 | * @return array key |
||
48 | */ |
||
49 | public function __get($key) |
||
54 | |||
55 | /** |
||
56 | * The magic setter method |
||
57 | * @params property, key |
||
58 | * @return array associative array properties |
||
59 | */ |
||
60 | public function __set($property, $value) |
||
65 | |||
66 | /** |
||
67 | * This method gets all the record from a particular table |
||
68 | * @params void |
||
69 | * @return associative array |
||
70 | * @throws NoRecordFoundException |
||
71 | */ |
||
72 | public static function getAll() |
||
84 | |||
85 | /** |
||
86 | * This method create or update record in a database table |
||
87 | * @params void |
||
88 | * @return bool true or false; |
||
89 | * @throws EmptyArrayException |
||
90 | * @throws NoRecordInsertionException |
||
91 | * @throws NoRecordUpdateException |
||
92 | */ |
||
93 | public function save() |
||
122 | |||
123 | /** |
||
124 | * This method find a record by id |
||
125 | * @params int id |
||
126 | * @return Object |
||
127 | * @throws NoArgumentPassedToFunctionException |
||
128 | */ |
||
129 | public static function find($id) |
||
146 | |||
147 | /** |
||
148 | * This method delete a row from the table by the row id |
||
149 | * @params int id |
||
150 | * @return boolean true or false |
||
151 | * @throws NoRecordDeletionException; |
||
152 | */ |
||
153 | public static function destroy($id) |
||
172 | |||
173 | /** |
||
174 | * This method return the current class name |
||
175 | * $params void |
||
176 | * @return classname |
||
177 | */ |
||
178 | public static function getClassName() |
||
187 | |||
188 | /** |
||
189 | * This method check if the argument passed to this function is an array |
||
190 | * @param $arrayOfRecord |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function checkIfRecordIsEmpty($arrayOfRecord) |
||
202 | |||
203 | } |
||
204 |
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.