Complex classes like Session often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Session, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
74 | class Session extends Component implements \IteratorAggregate, \ArrayAccess, \Countable |
||
75 | { |
||
76 | /** |
||
77 | * @var string the name of the session variable that stores the flash message data. |
||
78 | */ |
||
79 | public $flashParam = '__flash'; |
||
80 | /** |
||
81 | * @var \SessionHandlerInterface|array an object implementing the SessionHandlerInterface or a configuration array. If set, will be used to provide persistency instead of build-in methods. |
||
82 | */ |
||
83 | public $handler; |
||
84 | |||
85 | /** |
||
86 | * @var array parameter-value pairs to override default session cookie parameters that are used for session_set_cookie_params() function |
||
87 | * Array may have the following possible keys: 'lifetime', 'path', 'domain', 'secure', 'httponly' |
||
88 | * @see http://www.php.net/manual/en/function.session-set-cookie-params.php |
||
89 | */ |
||
90 | private $_cookieParams = ['httponly' => true]; |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Initializes the application component. |
||
95 | * This method is required by IApplicationComponent and is invoked by application. |
||
96 | */ |
||
97 | 33 | public function init() |
|
98 | { |
||
99 | 33 | parent::init(); |
|
100 | 33 | register_shutdown_function([$this, 'close']); |
|
101 | 33 | if ($this->getIsActive()) { |
|
102 | 4 | Yii::warning('Session is already started', __METHOD__); |
|
103 | 4 | $this->updateFlashCounters(); |
|
104 | 4 | } |
|
105 | 33 | } |
|
106 | |||
107 | /** |
||
108 | * Returns a value indicating whether to use custom session storage. |
||
109 | * This method should be overridden to return true by child classes that implement custom session storage. |
||
110 | * To implement custom session storage, override these methods: [[openSession()]], [[closeSession()]], |
||
111 | * [[readSession()]], [[writeSession()]], [[destroySession()]] and [[gcSession()]]. |
||
112 | * @return bool whether to use custom storage. |
||
113 | */ |
||
114 | 18 | public function getUseCustomStorage() |
|
115 | { |
||
116 | 18 | return false; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Starts the session. |
||
121 | */ |
||
122 | 23 | public function open() |
|
123 | { |
||
124 | 23 | if ($this->getIsActive()) { |
|
125 | 23 | return; |
|
126 | } |
||
127 | |||
128 | 19 | $this->registerSessionHandler(); |
|
129 | |||
130 | 19 | $this->setCookieParamsInternal(); |
|
131 | |||
132 | 19 | @session_start(); |
|
|
|||
133 | |||
134 | 19 | if ($this->getIsActive()) { |
|
135 | 19 | Yii::info('Session started', __METHOD__); |
|
136 | 19 | $this->updateFlashCounters(); |
|
137 | 19 | } else { |
|
138 | $error = error_get_last(); |
||
139 | $message = isset($error['message']) ? $error['message'] : 'Failed to start session.'; |
||
140 | Yii::error($message, __METHOD__); |
||
141 | } |
||
142 | 19 | } |
|
143 | |||
144 | /** |
||
145 | * Registers session handler. |
||
146 | * @throws \yii\base\InvalidConfigException |
||
147 | */ |
||
148 | 19 | protected function registerSessionHandler() |
|
149 | { |
||
150 | 19 | if ($this->handler !== null) { |
|
151 | if (!is_object($this->handler)) { |
||
152 | $this->handler = Yii::createObject($this->handler); |
||
153 | } |
||
154 | if (!$this->handler instanceof \SessionHandlerInterface) { |
||
155 | throw new InvalidConfigException('"' . get_class($this) . '::handler" must implement the SessionHandlerInterface.'); |
||
156 | } |
||
157 | YII_DEBUG ? session_set_save_handler($this->handler, false) : @session_set_save_handler($this->handler, false); |
||
158 | 19 | } elseif ($this->getUseCustomStorage()) { |
|
159 | 1 | if (YII_DEBUG) { |
|
160 | 1 | session_set_save_handler( |
|
161 | 1 | [$this, 'openSession'], |
|
162 | 1 | [$this, 'closeSession'], |
|
163 | 1 | [$this, 'readSession'], |
|
164 | 1 | [$this, 'writeSession'], |
|
165 | 1 | [$this, 'destroySession'], |
|
166 | 1 | [$this, 'gcSession'] |
|
167 | 1 | ); |
|
168 | 1 | } else { |
|
169 | @session_set_save_handler( |
||
170 | [$this, 'openSession'], |
||
171 | [$this, 'closeSession'], |
||
172 | [$this, 'readSession'], |
||
173 | [$this, 'writeSession'], |
||
174 | [$this, 'destroySession'], |
||
175 | [$this, 'gcSession'] |
||
176 | ); |
||
177 | } |
||
178 | 1 | } |
|
179 | 19 | } |
|
180 | |||
181 | /** |
||
182 | * Ends the current session and store session data. |
||
183 | */ |
||
184 | 27 | public function close() |
|
185 | { |
||
186 | 27 | if ($this->getIsActive()) { |
|
187 | 19 | YII_DEBUG ? session_write_close() : @session_write_close(); |
|
188 | 19 | } |
|
189 | 27 | } |
|
190 | |||
191 | /** |
||
192 | * Frees all session variables and destroys all data registered to a session. |
||
193 | */ |
||
194 | 1 | public function destroy() |
|
195 | { |
||
196 | 1 | if ($this->getIsActive()) { |
|
197 | 1 | $sessionId = session_id(); |
|
198 | 1 | $this->close(); |
|
199 | 1 | $this->setId($sessionId); |
|
200 | 1 | $this->open(); |
|
201 | 1 | session_unset(); |
|
202 | 1 | session_destroy(); |
|
203 | 1 | $this->setId($sessionId); |
|
204 | 1 | } |
|
205 | 1 | } |
|
206 | |||
207 | /** |
||
208 | * @return bool whether the session has started |
||
209 | */ |
||
210 | 33 | public function getIsActive() |
|
211 | { |
||
212 | 33 | return session_status() === PHP_SESSION_ACTIVE; |
|
213 | } |
||
214 | |||
215 | private $_hasSessionId; |
||
216 | |||
217 | /** |
||
218 | * Returns a value indicating whether the current request has sent the session ID. |
||
219 | * The default implementation will check cookie and $_GET using the session name. |
||
220 | * If you send session ID via other ways, you may need to override this method |
||
221 | * or call [[setHasSessionId()]] to explicitly set whether the session ID is sent. |
||
222 | * @return bool whether the current request has sent the session ID. |
||
223 | */ |
||
224 | 9 | public function getHasSessionId() |
|
225 | { |
||
226 | 9 | if ($this->_hasSessionId === null) { |
|
227 | 9 | $name = $this->getName(); |
|
228 | 9 | $request = Yii::$app->getRequest(); |
|
229 | 9 | if (!empty($_COOKIE[$name]) && ini_get('session.use_cookies')) { |
|
230 | $this->_hasSessionId = true; |
||
231 | 9 | } elseif (!ini_get('session.use_only_cookies') && ini_get('session.use_trans_sid')) { |
|
232 | $this->_hasSessionId = $request->get($name) != ''; |
||
233 | } else { |
||
234 | 9 | $this->_hasSessionId = false; |
|
235 | } |
||
236 | 9 | } |
|
237 | |||
238 | 9 | return $this->_hasSessionId; |
|
239 | } |
||
240 | |||
241 | /** |
||
242 | * Sets the value indicating whether the current request has sent the session ID. |
||
243 | * This method is provided so that you can override the default way of determining |
||
244 | * whether the session ID is sent. |
||
245 | * @param bool $value whether the current request has sent the session ID. |
||
246 | */ |
||
247 | public function setHasSessionId($value) |
||
248 | { |
||
249 | $this->_hasSessionId = $value; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Gets the session ID. |
||
254 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
255 | * @return string the current session ID |
||
256 | */ |
||
257 | 1 | public function getId() |
|
258 | { |
||
259 | 1 | return session_id(); |
|
260 | } |
||
261 | |||
262 | /** |
||
263 | * Sets the session ID. |
||
264 | * This is a wrapper for [PHP session_id()](http://php.net/manual/en/function.session-id.php). |
||
265 | * @param string $value the session ID for the current session |
||
266 | */ |
||
267 | 1 | public function setId($value) |
|
268 | { |
||
269 | 1 | session_id($value); |
|
270 | 1 | } |
|
271 | |||
272 | /** |
||
273 | * Updates the current session ID with a newly generated one . |
||
274 | * Please refer to <http://php.net/session_regenerate_id> for more details. |
||
275 | * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
||
276 | */ |
||
277 | public function regenerateID($deleteOldSession = false) |
||
278 | { |
||
279 | if ($this->getIsActive()) { |
||
280 | // add @ to inhibit possible warning due to race condition |
||
281 | // https://github.com/yiisoft/yii2/pull/1812 |
||
282 | if (YII_DEBUG && !headers_sent()) { |
||
283 | session_regenerate_id($deleteOldSession); |
||
284 | } else { |
||
285 | @session_regenerate_id($deleteOldSession); |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Gets the name of the current session. |
||
292 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
293 | * @return string the current session name |
||
294 | */ |
||
295 | 9 | public function getName() |
|
296 | { |
||
297 | 9 | return session_name(); |
|
298 | } |
||
299 | |||
300 | /** |
||
301 | * Sets the name for the current session. |
||
302 | * This is a wrapper for [PHP session_name()](http://php.net/manual/en/function.session-name.php). |
||
303 | * @param string $value the session name for the current session, must be an alphanumeric string. |
||
304 | * It defaults to "PHPSESSID". |
||
305 | */ |
||
306 | public function setName($value) |
||
307 | { |
||
308 | session_name($value); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Gets the current session save path. |
||
313 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
314 | * @return string the current session save path, defaults to '/tmp'. |
||
315 | */ |
||
316 | public function getSavePath() |
||
317 | { |
||
318 | return session_save_path(); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Sets the current session save path. |
||
323 | * This is a wrapper for [PHP session_save_path()](http://php.net/manual/en/function.session-save-path.php). |
||
324 | * @param string $value the current session save path. This can be either a directory name or a path alias. |
||
325 | * @throws InvalidParamException if the path is not a valid directory |
||
326 | */ |
||
327 | public function setSavePath($value) |
||
328 | { |
||
329 | $path = Yii::getAlias($value); |
||
330 | if (is_dir($path)) { |
||
331 | session_save_path($path); |
||
332 | } else { |
||
333 | throw new InvalidParamException("Session save path is not a valid directory: $value"); |
||
334 | } |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return array the session cookie parameters. |
||
339 | * @see http://php.net/manual/en/function.session-get-cookie-params.php |
||
340 | */ |
||
341 | 19 | public function getCookieParams() |
|
342 | { |
||
343 | 19 | return array_merge(session_get_cookie_params(), array_change_key_case($this->_cookieParams)); |
|
344 | } |
||
345 | |||
346 | /** |
||
347 | * Sets the session cookie parameters. |
||
348 | * The cookie parameters passed to this method will be merged with the result |
||
349 | * of `session_get_cookie_params()`. |
||
350 | * @param array $value cookie parameters, valid keys include: `lifetime`, `path`, `domain`, `secure` and `httponly`. |
||
351 | * @throws InvalidParamException if the parameters are incomplete. |
||
352 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
353 | */ |
||
354 | public function setCookieParams(array $value) |
||
355 | { |
||
356 | $this->_cookieParams = $value; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Sets the session cookie parameters. |
||
361 | * This method is called by [[open()]] when it is about to open the session. |
||
362 | * @throws InvalidParamException if the parameters are incomplete. |
||
363 | * @see http://us2.php.net/manual/en/function.session-set-cookie-params.php |
||
364 | */ |
||
365 | 19 | private function setCookieParamsInternal() |
|
366 | { |
||
367 | 19 | $data = $this->getCookieParams(); |
|
368 | 19 | if (isset($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly'])) { |
|
369 | 19 | session_set_cookie_params($data['lifetime'], $data['path'], $data['domain'], $data['secure'], $data['httponly']); |
|
370 | 19 | } else { |
|
371 | throw new InvalidParamException('Please make sure cookieParams contains these elements: lifetime, path, domain, secure and httponly.'); |
||
372 | } |
||
373 | 19 | } |
|
374 | |||
375 | /** |
||
376 | * Returns the value indicating whether cookies should be used to store session IDs. |
||
377 | * @return bool|null the value indicating whether cookies should be used to store session IDs. |
||
378 | * @see setUseCookies() |
||
379 | */ |
||
380 | public function getUseCookies() |
||
390 | |||
391 | /** |
||
392 | * Sets the value indicating whether cookies should be used to store session IDs. |
||
393 | * Three states are possible: |
||
394 | * |
||
395 | * - true: cookies and only cookies will be used to store session IDs. |
||
396 | * - false: cookies will not be used to store session IDs. |
||
397 | * - null: if possible, cookies will be used to store session IDs; if not, other mechanisms will be used (e.g. GET parameter) |
||
398 | * |
||
399 | * @param bool|null $value the value indicating whether cookies should be used to store session IDs. |
||
400 | */ |
||
401 | public function setUseCookies($value) |
||
414 | |||
415 | /** |
||
416 | * @return float the probability (percentage) that the GC (garbage collection) process is started on every session initialization, defaults to 1 meaning 1% chance. |
||
417 | */ |
||
418 | public function getGCProbability() |
||
422 | |||
423 | /** |
||
424 | * @param float $value the probability (percentage) that the GC (garbage collection) process is started on every session initialization. |
||
425 | * @throws InvalidParamException if the value is not between 0 and 100. |
||
426 | */ |
||
427 | public function setGCProbability($value) |
||
437 | |||
438 | /** |
||
439 | * @return bool whether transparent sid support is enabled or not, defaults to false. |
||
440 | */ |
||
441 | public function getUseTransparentSessionID() |
||
445 | |||
446 | /** |
||
447 | * @param bool $value whether transparent sid support is enabled or not. |
||
448 | */ |
||
449 | public function setUseTransparentSessionID($value) |
||
453 | |||
454 | /** |
||
455 | * @return int the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
456 | * The default value is 1440 seconds (or the value of "session.gc_maxlifetime" set in php.ini). |
||
457 | */ |
||
458 | 11 | public function getTimeout() |
|
462 | |||
463 | /** |
||
464 | * @param int $value the number of seconds after which data will be seen as 'garbage' and cleaned up |
||
465 | */ |
||
466 | public function setTimeout($value) |
||
470 | |||
471 | /** |
||
472 | * Session open handler. |
||
473 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
474 | * @internal Do not call this method directly. |
||
475 | * @param string $savePath session save path |
||
476 | * @param string $sessionName session name |
||
477 | * @return bool whether session is opened successfully |
||
478 | */ |
||
479 | 7 | public function openSession($savePath, $sessionName) |
|
480 | { |
||
481 | 7 | return true; |
|
482 | } |
||
483 | |||
484 | /** |
||
485 | * Session close handler. |
||
486 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
487 | * @internal Do not call this method directly. |
||
488 | * @return bool whether session is closed successfully |
||
489 | */ |
||
490 | 7 | public function closeSession() |
|
491 | { |
||
492 | 7 | return true; |
|
493 | } |
||
494 | |||
495 | /** |
||
496 | * Session read handler. |
||
497 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
498 | * @internal Do not call this method directly. |
||
499 | * @param string $id session ID |
||
500 | * @return string the session data |
||
501 | */ |
||
502 | public function readSession($id) |
||
506 | |||
507 | /** |
||
508 | * Session write handler. |
||
509 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
510 | * @internal Do not call this method directly. |
||
511 | * @param string $id session ID |
||
512 | * @param string $data session data |
||
513 | * @return bool whether session write is successful |
||
514 | */ |
||
515 | public function writeSession($id, $data) |
||
519 | |||
520 | /** |
||
521 | * Session destroy handler. |
||
522 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
523 | * @internal Do not call this method directly. |
||
524 | * @param string $id session ID |
||
525 | * @return bool whether session is destroyed successfully |
||
526 | */ |
||
527 | public function destroySession($id) |
||
531 | |||
532 | /** |
||
533 | * Session GC (garbage collection) handler. |
||
534 | * This method should be overridden if [[useCustomStorage]] returns true. |
||
535 | * @internal Do not call this method directly. |
||
536 | * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up. |
||
537 | * @return bool whether session is GCed successfully |
||
538 | */ |
||
539 | public function gcSession($maxLifetime) |
||
543 | |||
544 | /** |
||
545 | * Returns an iterator for traversing the session variables. |
||
546 | * This method is required by the interface [[\IteratorAggregate]]. |
||
547 | * @return SessionIterator an iterator for traversing the session variables. |
||
548 | */ |
||
549 | public function getIterator() |
||
554 | |||
555 | /** |
||
556 | * Returns the number of items in the session. |
||
557 | * @return int the number of session variables |
||
558 | */ |
||
559 | public function getCount() |
||
564 | |||
565 | /** |
||
566 | * Returns the number of items in the session. |
||
567 | * This method is required by [[\Countable]] interface. |
||
568 | * @return int number of items in the session. |
||
569 | */ |
||
570 | public function count() |
||
574 | |||
575 | /** |
||
576 | * Returns the session variable value with the session variable name. |
||
577 | * If the session variable does not exist, the `$defaultValue` will be returned. |
||
578 | * @param string $key the session variable name |
||
579 | * @param mixed $defaultValue the default value to be returned when the session variable does not exist. |
||
580 | * @return mixed the session variable value, or $defaultValue if the session variable does not exist. |
||
581 | */ |
||
582 | 23 | public function get($key, $defaultValue = null) |
|
587 | |||
588 | /** |
||
589 | * Adds a session variable. |
||
590 | * If the specified name already exists, the old value will be overwritten. |
||
591 | * @param string $key session variable name |
||
592 | * @param mixed $value session variable value |
||
593 | */ |
||
594 | 18 | public function set($key, $value) |
|
599 | |||
600 | /** |
||
601 | * Removes a session variable. |
||
602 | * @param string $key the name of the session variable to be removed |
||
603 | * @return mixed the removed value, null if no such session variable. |
||
604 | */ |
||
605 | 13 | public function remove($key) |
|
617 | |||
618 | /** |
||
619 | * Removes all session variables |
||
620 | */ |
||
621 | 3 | public function removeAll() |
|
628 | |||
629 | /** |
||
630 | * @param mixed $key session variable name |
||
631 | * @return bool whether there is the named session variable |
||
632 | */ |
||
633 | public function has($key) |
||
638 | |||
639 | /** |
||
640 | * Updates the counters for flash messages and removes outdated flash messages. |
||
641 | * This method should only be called once in [[init()]]. |
||
642 | */ |
||
643 | 23 | protected function updateFlashCounters() |
|
660 | |||
661 | /** |
||
662 | * Returns a flash message. |
||
663 | * @param string $key the key identifying the flash message |
||
664 | * @param mixed $defaultValue value to be returned if the flash message does not exist. |
||
665 | * @param bool $delete whether to delete this flash message right after this method is called. |
||
666 | * If false, the flash message will be automatically deleted in the next request. |
||
667 | * @return mixed the flash message or an array of messages if addFlash was used |
||
668 | * @see setFlash() |
||
669 | * @see addFlash() |
||
670 | * @see hasFlash() |
||
671 | * @see getAllFlashes() |
||
672 | * @see removeFlash() |
||
673 | */ |
||
674 | public function getFlash($key, $defaultValue = null, $delete = false) |
||
692 | |||
693 | /** |
||
694 | * Returns all flash messages. |
||
695 | * |
||
696 | * You may use this method to display all the flash messages in a view file: |
||
697 | * |
||
698 | * ```php |
||
699 | * <?php |
||
700 | * foreach (Yii::$app->session->getAllFlashes() as $key => $message) { |
||
701 | * echo '<div class="alert alert-' . $key . '">' . $message . '</div>'; |
||
702 | * } ?> |
||
703 | * ``` |
||
704 | * |
||
705 | * With the above code you can use the [bootstrap alert][] classes such as `success`, `info`, `danger` |
||
706 | * as the flash message key to influence the color of the div. |
||
707 | * |
||
708 | * Note that if you use [[addFlash()]], `$message` will be an array, and you will have to adjust the above code. |
||
709 | * |
||
710 | * [bootstrap alert]: http://getbootstrap.com/components/#alerts |
||
711 | * |
||
712 | * @param bool $delete whether to delete the flash messages right after this method is called. |
||
713 | * If false, the flash messages will be automatically deleted in the next request. |
||
714 | * @return array flash messages (key => message or key => [message1, message2]). |
||
715 | * @see setFlash() |
||
716 | * @see addFlash() |
||
717 | * @see getFlash() |
||
718 | * @see hasFlash() |
||
719 | * @see removeFlash() |
||
720 | */ |
||
721 | public function getAllFlashes($delete = false) |
||
743 | |||
744 | /** |
||
745 | * Sets a flash message. |
||
746 | * A flash message will be automatically deleted after it is accessed in a request and the deletion will happen |
||
747 | * in the next request. |
||
748 | * If there is already an existing flash message with the same key, it will be overwritten by the new one. |
||
749 | * @param string $key the key identifying the flash message. Note that flash messages |
||
750 | * and normal session variables share the same name space. If you have a normal |
||
751 | * session variable using the same name, its value will be overwritten by this method. |
||
752 | * @param mixed $value flash message |
||
753 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
754 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
755 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
756 | * it is accessed. |
||
757 | * @see getFlash() |
||
758 | * @see addFlash() |
||
759 | * @see removeFlash() |
||
760 | */ |
||
761 | public function setFlash($key, $value = true, $removeAfterAccess = true) |
||
768 | |||
769 | /** |
||
770 | * Adds a flash message. |
||
771 | * If there are existing flash messages with the same key, the new one will be appended to the existing message array. |
||
772 | * @param string $key the key identifying the flash message. |
||
773 | * @param mixed $value flash message |
||
774 | * @param bool $removeAfterAccess whether the flash message should be automatically removed only if |
||
775 | * it is accessed. If false, the flash message will be automatically removed after the next request, |
||
776 | * regardless if it is accessed or not. If true (default value), the flash message will remain until after |
||
777 | * it is accessed. |
||
778 | * @see getFlash() |
||
779 | * @see setFlash() |
||
780 | * @see removeFlash() |
||
781 | */ |
||
782 | public function addFlash($key, $value = true, $removeAfterAccess = true) |
||
797 | |||
798 | /** |
||
799 | * Removes a flash message. |
||
800 | * @param string $key the key identifying the flash message. Note that flash messages |
||
801 | * and normal session variables share the same name space. If you have a normal |
||
802 | * session variable using the same name, it will be removed by this method. |
||
803 | * @return mixed the removed flash message. Null if the flash message does not exist. |
||
804 | * @see getFlash() |
||
805 | * @see setFlash() |
||
806 | * @see addFlash() |
||
807 | * @see removeAllFlashes() |
||
808 | */ |
||
809 | public function removeFlash($key) |
||
818 | |||
819 | /** |
||
820 | * Removes all flash messages. |
||
821 | * Note that flash messages and normal session variables share the same name space. |
||
822 | * If you have a normal session variable using the same name, it will be removed |
||
823 | * by this method. |
||
824 | * @see getFlash() |
||
825 | * @see setFlash() |
||
826 | * @see addFlash() |
||
827 | * @see removeFlash() |
||
828 | */ |
||
829 | public function removeAllFlashes() |
||
837 | |||
838 | /** |
||
839 | * Returns a value indicating whether there are flash messages associated with the specified key. |
||
840 | * @param string $key key identifying the flash message type |
||
841 | * @return bool whether any flash messages exist under specified key |
||
842 | */ |
||
843 | public function hasFlash($key) |
||
847 | |||
848 | /** |
||
849 | * This method is required by the interface [[\ArrayAccess]]. |
||
850 | * @param mixed $offset the offset to check on |
||
851 | * @return bool |
||
852 | */ |
||
853 | public function offsetExists($offset) |
||
859 | |||
860 | /** |
||
861 | * This method is required by the interface [[\ArrayAccess]]. |
||
862 | * @param int $offset the offset to retrieve element. |
||
863 | * @return mixed the element at the offset, null if no element is found at the offset |
||
864 | */ |
||
865 | public function offsetGet($offset) |
||
871 | |||
872 | /** |
||
873 | * This method is required by the interface [[\ArrayAccess]]. |
||
874 | * @param int $offset the offset to set element |
||
875 | * @param mixed $item the element value |
||
876 | */ |
||
877 | public function offsetSet($offset, $item) |
||
882 | |||
883 | /** |
||
884 | * This method is required by the interface [[\ArrayAccess]]. |
||
885 | * @param mixed $offset the offset to unset element |
||
886 | */ |
||
887 | public function offsetUnset($offset) |
||
892 | } |
||
893 |
If you suppress an error, we recommend checking for the error condition explicitly: