1 | <?php |
||
9 | class Query extends QueryHandler |
||
10 | { |
||
11 | /** Class name for interacting with database */ |
||
12 | protected $class_name; |
||
13 | |||
14 | /** |
||
15 | * Collection of query parameters objects |
||
16 | * @see \samson\activerecord\QueryParams |
||
17 | */ |
||
18 | protected $parameters = array(); |
||
19 | |||
20 | /** |
||
21 | * Reset all query parameters |
||
22 | * @return self Chaining |
||
23 | */ |
||
24 | public function flush() |
||
32 | |||
33 | /** |
||
34 | * Perform database request and get collection of database record objects |
||
35 | * @see \samson\activerecord\Query::execute() |
||
36 | * @param mixed $return External variable to store query results |
||
37 | * @return mixed If no arguments passed returns query results collection, otherwise query success status |
||
38 | */ |
||
39 | public function exec(& $return = null) |
||
44 | |||
45 | /** |
||
46 | * Perform database request and get first record from results collection |
||
47 | * @see \samson\activerecord\Query::execute() |
||
48 | * @param mixed $return External variable to store query results |
||
49 | * @return mixed If no arguments passed returns query results first database record object, |
||
50 | * otherwise query success status |
||
51 | */ |
||
52 | public function first(& $return = null) |
||
57 | |||
58 | /** |
||
59 | * Perform database request and get array of record field values |
||
60 | * @see \samson\activerecord\Query::execute() |
||
61 | * @param string $fieldName Record field name to get value from |
||
62 | * @param string $return External variable to store query results |
||
63 | * @return Ambigous <boolean, NULL, mixed> |
||
64 | */ |
||
65 | public function fields($fieldName, & $return = null) |
||
82 | |||
83 | /** |
||
84 | * Perform database request and return different results depending on function arguments. |
||
85 | * @see \samson\activerecord\Record |
||
86 | * @param array $result External variable to store dabatase request results collection |
||
87 | * @param integer|bool $rType Amount of arguments passed to parent function |
||
88 | * @param integer $limit Quantity of records to return |
||
89 | * @param callable $handler External callable handler for results modification |
||
90 | * @param array $handlerArgs External callable handler arguments |
||
91 | * @return boolean/array Boolean if $r_type > 0, otherwise array of request results |
||
92 | */ |
||
93 | protected function & execute( |
||
143 | } |
||
144 |
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.