Skip to content

Instantly share code, notes, and snippets.

@BaylorRae
Created August 31, 2012 21:15
Show Gist options
  • Select an option

  • Save BaylorRae/3559115 to your computer and use it in GitHub Desktop.

Select an option

Save BaylorRae/3559115 to your computer and use it in GitHub Desktop.
<?php include 'flash.php'; ?>
...
<?php foreach( Flash::$messages as $id => $msg ) : ?>
<div class="flash_<?php echo $id ?>"><?php echo $msg ?></div>
<?php endforeach; ?>
<?php
// make sure sessions work on the page
session_start();
class Flash {
// where all messages are stored
public static $messages = array();
/*
* A generic function to store flash messages
*
* Flash::add('notice', 'a message to display');
*
* @param string $name the name/id of the flash
* @param string $message the message to display
*/
public static function add($name, $message) {
$_SESSION['flash_messages'][$name] = $message;
}
/*
* A shortcut to Flash::add()
*
* Flash::notice('a message to display');
*/
public static function __callStatic($fn, $args) {
call_user_func_array(array('Flash', 'add'), array($fn, $args[0]));
}
}
// if $_SESSION['flash_messages'] isset
// then save them to our class
if( isset($_SESSION['flash_messages']) ) {
self::$messages = $_SESSION['flash_messages'];
}
// reset the session's value
$_SESSION['flash_messages'] = array();
<?php
include 'flash.php';
// using Flash::add directly
Flash::add('notice', 'Awesome job dude!');
// using the alias
Flash::alert('lol, you can\'t do that!');
// calls: Flash::add('alert', 'lol, you can\'t do that!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment