| 1 | <?php |
||
| 15 | class MemcachedCache extends CacheItemPool |
||
| 16 | { |
||
| 17 | public function __construct(Memcached $memcached) |
||
| 18 | { |
||
| 19 | parent::__construct(new \Doctrine\Common\Cache\MemcachedCache()); |
||
| 20 | $this->provider->setMemcached($memcached); |
||
|
|
|||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Sets the memcache instance to use. |
||
| 25 | * |
||
| 26 | * @param Memcached $memcached |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function setMemcached(Memcached $memcached) |
||
| 34 | /** |
||
| 35 | * Gets the memcached instance used by the cache. |
||
| 36 | * |
||
| 37 | * @return Memcached|null |
||
| 38 | */ |
||
| 39 | public function getMemcached() |
||
| 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: