1 | <?php |
||
20 | class Session implements \IteratorAggregate |
||
21 | { |
||
22 | use SingletonTrait; |
||
23 | |||
24 | /** @var array The session. */ |
||
25 | private $session; |
||
26 | |||
27 | /** |
||
28 | * Construct a Sesssion object. |
||
29 | * |
||
30 | * @global array $_SESSION The session parameters. |
||
31 | */ |
||
32 | 13 | protected function __construct() |
|
36 | |||
37 | /** |
||
38 | * Initialize the session, if none exists. |
||
39 | * |
||
40 | * @param $name = null |
||
41 | * @return null |
||
42 | * @throws \RuntimeException on failure. |
||
43 | */ |
||
44 | 4 | public static function start($name = null) |
|
54 | |||
55 | /** |
||
56 | * Destroy the session, if one exists. |
||
57 | * |
||
58 | * @return null |
||
59 | * @throws \RuntimeException on failure. |
||
60 | */ |
||
61 | 2 | public static function destroy() |
|
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 1 | public function getIterator() |
|
75 | |||
76 | /** |
||
77 | * Returns the number of key-value mappings in the session map. |
||
78 | * |
||
79 | * @return int the number of key-value mappings in the session map. |
||
80 | */ |
||
81 | 1 | public static function count() |
|
85 | |||
86 | /** |
||
87 | * Returns true if the session map contains no key-value mappings. |
||
88 | * |
||
89 | * @return bool true if the session map contains no key-value mappings. |
||
90 | */ |
||
91 | 2 | public static function isEmpty() |
|
95 | |||
96 | /** |
||
97 | * Returns true if the session map contains a mapping for the specified key. |
||
98 | * |
||
99 | * @param string $key |
||
100 | * @return bool true if the session map contains a mapping for the specified key. |
||
101 | */ |
||
102 | 4 | public static function containsKey($key) |
|
106 | |||
107 | /** |
||
108 | * Returns true if the session map maps one or more keys to the specified value. |
||
109 | * |
||
110 | * @param string $value |
||
111 | * @return bool true if the session map maps one or more keys to the specified value. |
||
112 | */ |
||
113 | 1 | public static function containsValue($value) |
|
117 | |||
118 | /** |
||
119 | * Returns the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
||
120 | * |
||
121 | * @param string $key |
||
122 | * @return string|null the value to which the specified key is mapped, or null if the session map contains no mapping for the key. |
||
123 | */ |
||
124 | 2 | public static function get($key) |
|
128 | |||
129 | /** |
||
130 | * Associates the specified value with the specified key in the session map. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param string $value |
||
134 | * @return null |
||
135 | */ |
||
136 | 8 | public static function set($key, $value) |
|
140 | |||
141 | /** |
||
142 | * Removes the mapping for the specified key from the session map if present. |
||
143 | * |
||
144 | * @param string $key |
||
145 | * @return null |
||
146 | */ |
||
147 | 1 | public static function remove($key) |
|
153 | |||
154 | /** |
||
155 | * Removes all of the mappings from the session map. |
||
156 | * |
||
157 | * @return null |
||
158 | */ |
||
159 | 1 | public static function clear() |
|
163 | } |
||
164 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: