Code: Select all
<?php
class Notify {
private static $error = array();
private static $success = array();
private static $info = array();
private static $log = array();
private static $instance;
private function __construct() {
self::log("Class Notify has been loaded.");
if(isset($_SESSION['success'])){
foreach($_SESSION['success'] as $message){
self::addSuccess($message);
}
unset($_SESSION['success']);
}
if(isset($_SESSION['error'])){
foreach($_SESSION['error'] as $message){
self::addError($message);
}
unset($_SESSION['error']);
}
if(isset($_SESSION['info'])){
foreach($_SESSION['info'] as $message){
self::addInfo($message);
}
unset($_SESSION['info']);
}
}
/**
* This is the method that wil be used to create a singleton of this object.
* @return type
*/
public function getInstance(){
if(!self::$instance instanceof self){
self::$instance = new self();
}
return self::$instance;
}
/**
* This is a simple helper method to return a given stack. The currently
* accepted stacks are: success, error and info. The $type has a default of
* info.
* @param type $type
*/
public function getStack($type='info'){
switch($type){
case 'log':
self::getLog();
break;
case 'success':
self::getSuccess();
break;
case 'error':
self::getError();
break;
case 'info':default:
self::getInfo();
break;
}
}
/**
* This method will return the log stack
* @return type
*/
public function getLog(){
return self::$log;
}
/**
* This method will return the error stack
* @return type
*/
public function getError(){
return self::$error;
}
/**
* This method will return the success stack
* @return type
*/
public function getSuccess(){
return self::$success;
}
/**
* This method will return the info stack.
* @return type
*/
public function getInfo(){
return self::$info;
}
/**
* This method will add a message to corresponding stack, which
* can be provided in the $type. $type currently accepts 3 different
* types: success, error, info. The default will be info.
* @param type $message
* @param type $type
*/
public function add($message, $type = 'info'){
switch($type){
case 'log':
self::addLog($message);
break;
case 'success':
self::addSuccess($message);
break;
case 'error':
self::addError($message);
break;
case 'info':default:
self::addInfo($message);
break;
}
}
/**
* This method will ad a message to the log stack
* @param type $message
*/
public function addLog($message){
array_push(self::$log, $message);
}
public function log($message){
self::addLog($message);
}
/**
* This method will ad a message to the info stack
* @param type $message
*/
public function addInfo($message){
array_push(self::$info, $message);
}
public function info($message){
self::addInfo($message);
}
/**
* This method will add a message to the success stack
* @param type $message
*/
public function addSuccess($message){
array_push(self::$success, $message);
}
public function success($message){
self::addSuccess($message);
}
/**
* This method will add a message to the error stack
* @param type $message
*/
public function addError($message){
array_push(self::$error, $message);
}
public function error($message){
self::addError($message);
}
}
To use it:
Code: Select all
require "path/to/Notify.php";
$notify = Notify::getInstance();
Code: Select all
Notify::success("This message will be added to the success stack!");
// or
$notify->success("This message will also be added to the success stack.");
Code: Select all
$successMessages = Notify::getSuccess();
// or
$successMessages = $notify->getStack("success");
Also, if you want to redirect after something, but still want to add a message to the stack, you can use sessions:
Code: Select all
session_start(); // should be on obviously
$_SESSION['success'][0] = "This is the first message given!";
$_SESSION['success'][1] = "This is the second message given!";
header("Location: index.php");
Code: Select all
$successMessages = Notify::getSuccess();
foreach($successMessages as $message){
echo "<p>". $message . "</p>";
}
// or use it like this
foreach(Notify::getSuccess() as $message){
echo "<p>" . $message . "</p>";
}