1 | <?php |
||
20 | abstract class Cache implements CacheItemPoolInterface |
||
21 | { |
||
22 | /** |
||
23 | * The options for the cache object. |
||
24 | * |
||
25 | * @var array|\ArrayAccess |
||
26 | * @since 1.0 |
||
27 | */ |
||
28 | protected $options; |
||
29 | |||
30 | /** |
||
31 | * The deferred items to store |
||
32 | * |
||
33 | * @var array |
||
34 | * @since 1.0 |
||
35 | */ |
||
36 | private $deferred = []; |
||
37 | |||
38 | /** |
||
39 | * Constructor. |
||
40 | * |
||
41 | * @param array|\ArrayAccess $options An options array, or an object that implements \ArrayAccess |
||
42 | * |
||
43 | * @since 1.0 |
||
44 | * @throws \RuntimeException |
||
45 | */ |
||
46 | 58 | public function __construct($options = []) |
|
47 | { |
||
48 | 58 | if (!($options instanceof \ArrayAccess || is_array($options))) |
|
49 | 58 | { |
|
50 | throw new InvalidArgumentException(sprintf('%s requires an options array or an object that implements \\ArrayAccess', __CLASS__)); |
||
51 | } |
||
52 | |||
53 | 58 | $this->options = $options; |
|
54 | 58 | } |
|
55 | |||
56 | /** |
||
57 | * Returns a traversable set of cache items. |
||
58 | * |
||
59 | * @param array $keys A list of keys that can obtained in a single operation. |
||
60 | * |
||
61 | * @return CacheItemInterface[] An associative array of CacheItemInterface objects keyed on the cache key. |
||
62 | * |
||
63 | * @since 1.0 |
||
64 | */ |
||
65 | 4 | public function getItems(array $keys = []) |
|
76 | |||
77 | /** |
||
78 | * Get an option from the Cache instance. |
||
79 | * |
||
80 | * @param string $key The name of the option to get. |
||
81 | * |
||
82 | * @return mixed The option value. |
||
83 | * |
||
84 | * @since 1.0 |
||
85 | */ |
||
86 | 12 | public function getOption($key) |
|
90 | |||
91 | /** |
||
92 | * Removes the item from the pool. |
||
93 | * |
||
94 | * @param string $key The key for which to delete |
||
95 | * |
||
96 | * @return boolean |
||
97 | * |
||
98 | * @since 1.0 |
||
99 | */ |
||
100 | abstract public function deleteItem($key); |
||
101 | |||
102 | /** |
||
103 | * Removes multiple items from the pool. |
||
104 | * |
||
105 | * @param array $keys An array of keys that should be removed from the pool. |
||
106 | * |
||
107 | * @return boolean |
||
108 | * |
||
109 | * @since 1.0 |
||
110 | */ |
||
111 | 6 | public function deleteItems(array $keys) |
|
125 | |||
126 | /** |
||
127 | * Set an option for the Cache instance. |
||
128 | * |
||
129 | * @param string $key The name of the option to set. |
||
130 | * @param mixed $value The option value to set. |
||
131 | * |
||
132 | * @return $this |
||
133 | * |
||
134 | * @since 1.0 |
||
135 | */ |
||
136 | 6 | public function setOption($key, $value) |
|
142 | |||
143 | /** |
||
144 | * Sets a cache item to be persisted later. |
||
145 | * |
||
146 | * @param CacheItemInterface $item The cache item to save. |
||
147 | * |
||
148 | * @return $this |
||
149 | * |
||
150 | * @since __DEPLOY_VERSION__ |
||
151 | */ |
||
152 | public function saveDeferred(CacheItemInterface $item) |
||
158 | |||
159 | /** |
||
160 | * Persists any deferred cache items. |
||
161 | * |
||
162 | * @return boolean |
||
163 | * |
||
164 | * @since __DEPLOY_VERSION__ |
||
165 | */ |
||
166 | public function commit() |
||
184 | |||
185 | /** |
||
186 | * Converts a DateTime object from the cache item to the expiry time in seconds from the present |
||
187 | * |
||
188 | * @param HasExpirationDateInterface $item The cache item |
||
189 | * |
||
190 | * @return integer The time in seconds until expiry |
||
191 | * |
||
192 | * @since __DEPLOY_VERSION__ |
||
193 | */ |
||
194 | 2 | protected function convertItemExpiryToSeconds(HasExpirationDateInterface $item) |
|
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.