1 | <?php |
||
15 | class MemcacheCache extends CacheItemPool |
||
16 | { |
||
17 | public function __construct(Memcache $memcached) |
||
18 | { |
||
19 | parent::__construct(new \Doctrine\Common\Cache\MemcacheCache()); |
||
20 | $this->provider->setMemcache($memcached); |
||
|
|||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Sets the memcache instance to use. |
||
25 | * |
||
26 | * @param Memcache $memcache |
||
27 | * |
||
28 | * @return void |
||
29 | */ |
||
30 | public function setMemcache(Memcache $memcache) |
||
34 | /** |
||
35 | * Gets the memcached instance used by the cache. |
||
36 | * |
||
37 | * @return Memcache|null |
||
38 | */ |
||
39 | public function getMemcache() |
||
43 | } |
||
44 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: