Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created May 9, 2018 14:32
Show Gist options
  • Save kobus1998/c3c990e6d9a85997175dab83dc1a186e to your computer and use it in GitHub Desktop.
Save kobus1998/c3c990e6d9a85997175dab83dc1a186e to your computer and use it in GitHub Desktop.
Flash messages
<?php
class FlashMsg {
const SUCCESS = 'success';
const WARNING = 'warning';
const ERROR = 'error';
/**
* Static msgs holder
* @var array
*/
private static $msgs = [
'success' => [],
'warning' => [],
'error' => []
];
/**
* Session save place
* @var string
*/
private static $savePlace = 'flash';
/**
* Set save place
* @param string saveplace
* @return void
*/
public static function setSavePlace($savePlace = 'flash')
{
self::$savePlace = $savePlace;
}
/**
* Set msgs into session
* @return void
*/
public static function set()
{
$_SESSION[self::$savePlace] = self::$msgs;
}
/**
* Save into object
* @param string const of object
* @param mixed anything you want to save
* @return void
*/
public static function save($type = 'success', $msg)
{
self::$msgs[$type][] = $msg;
}
/**
* Clear session
* @return void
*/
public static function clear()
{
self::$msgs['success'] = [];
self::$msgs['warning'] = [];
self::$msgs['error'] = [];
unset($_SESSION[self::$savePlace]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment