1 | <?php |
||
9 | abstract class PagedLazyResourceList extends LazyResourceList |
||
10 | { |
||
11 | /* @var integer */ |
||
12 | protected $position = 0; |
||
13 | |||
14 | /** |
||
15 | * Convert a retrieved resource object to a model object. |
||
16 | * |
||
17 | * @param $data |
||
18 | * @return mixed |
||
19 | */ |
||
20 | abstract protected function convertToResource($data); |
||
21 | |||
22 | /** |
||
23 | * Fetch more resources. |
||
24 | * |
||
25 | * @return mixed |
||
26 | */ |
||
27 | abstract protected function fetchBatch(); |
||
28 | |||
29 | /** |
||
30 | * Fetch all the data. |
||
31 | */ |
||
32 | protected function fetchData() |
||
36 | |||
37 | /** |
||
38 | * Rewind the Iterator to the first element. |
||
39 | * |
||
40 | * @link http://php.net/manual/en/iterator.rewind.php |
||
41 | * @return void |
||
42 | */ |
||
43 | public function rewind() |
||
47 | |||
48 | /** |
||
49 | * Checks if current position is valid. |
||
50 | * |
||
51 | * @link http://php.net/manual/en/iterator.valid.php |
||
52 | * @return boolean |
||
53 | */ |
||
54 | public function valid() |
||
62 | |||
63 | /** |
||
64 | * Return the current element |
||
65 | * @link http://php.net/manual/en/iterator.current.php |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function current() |
||
72 | |||
73 | /** |
||
74 | * Move forward to next element |
||
75 | * @link http://php.net/manual/en/iterator.next.php |
||
76 | * @return void |
||
77 | */ |
||
78 | public function next() |
||
82 | |||
83 | /** |
||
84 | * Return the key of the current element |
||
85 | * @link http://php.net/manual/en/iterator.key.php |
||
86 | * @return integer|null Scalar on success, or null on failure. |
||
87 | */ |
||
88 | public function key() |
||
92 | } |
||
93 |
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.