These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Pimf |
||
4 | * |
||
5 | * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
||
6 | * @license http://opensource.org/licenses/MIT MIT License |
||
7 | */ |
||
8 | |||
9 | namespace Pimf\Session\Storages; |
||
10 | |||
11 | /** |
||
12 | * @package Session_Storages |
||
13 | * @author Gjero Krsteski <[email protected]> |
||
14 | */ |
||
15 | class Memcached extends Storage |
||
16 | { |
||
17 | /** |
||
18 | * The Memcache cache storage instance. |
||
19 | * |
||
20 | * @var \Pimf\Cache\Storages\Memcached |
||
21 | */ |
||
22 | private $memcached; |
||
23 | |||
24 | /** |
||
25 | * @param \Pimf\Cache\Storages\Memcached $memcached |
||
26 | */ |
||
27 | public function __construct(\Pimf\Cache\Storages\Memcached $memcached) |
||
28 | { |
||
29 | $this->memcached = $memcached; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Load a session from storage by a given ID. |
||
34 | * |
||
35 | * @param string $key |
||
36 | * |
||
37 | * @return array|mixed|null |
||
38 | */ |
||
39 | public function load($key) |
||
40 | { |
||
41 | return $this->memcached->get($key); |
||
0 ignored issues
–
show
|
|||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Save a given session to storage. |
||
46 | * |
||
47 | * @param array $session |
||
48 | * @param array $config |
||
49 | * @param bool $exists |
||
50 | */ |
||
51 | public function save($session, $config, $exists) |
||
52 | { |
||
53 | $this->memcached->put($session['id'], $session, $config['lifetime']); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param string $key |
||
58 | */ |
||
59 | public function delete($key) |
||
60 | { |
||
61 | $this->memcached->forget($key); |
||
62 | } |
||
63 | } |
||
64 |
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.