Consider this:
class ActionHandler {
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------
This class will be a singleton class structure by calling its constructor by reference only (prefixed by '&'). Is primarly used to
reference its errorArray Array property as a single reference to allow for static usage
--------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
var $isSuccessful = true; // BOOLEAN PROPERTY DETERMINING IF ACTION WAS SUCCESSFUL
var $tableName; // DB TABLE NAME PROPERTY
var $fileName; // FILE NAME PROPERTY
var $dbConn; // DB CONNECTION OBJECT
var $dbConnObj; // DB RESOURCE LINK OBJECT
/*---------------------------------------------------------------------------------------------------------------------------------------------------------
Remember that you since this class will be extended by other classes especially in action.inc.php, for properties to
have value into the class this extends into, you will have to not "construct" them here since this constructor will not
be called directly by the extended class.
------------------------------------------------------------------------------------------------------------------------------------------------------------*/
function &ActionHandler() { // SINGLETON CONSTRUCTOR
static $errorArray = array(); // ASSOCIATIVE ARRAY PROPERTY FIELD => ERROR MSG
}
//----------------------------------------------------* GETTER/SETTER METHODS *--------------------------------------------------------------
function &getErrorArray() { // STATIC ARRAY METHOD
return $this->errorArray;
}
function &getHTMLRedirect($myImmedAction, $additionalQS = '') { // STATIC HTML/JAVASCRIPT STRING METHOD
global $section, $clientFolderName, $projectFolderName, $redirectArray;
if ($redirectArray[$section][$myImmedAction] && $myImmedAction) {
$html = "\n<script>\n <!--\n location.href = '/$clientFolderName/$projectFolderName/index.php?section=$section&action=" .
$redirectArray[$section][$myImmedAction] . '&success=' . urlencode($this->getSuccessMsg());
$html .= ($additionalQS) ? '&' . urlencode($additionalQS) : '';
$html .= "';\n //-->\n</script>\n<noscript>\n<meta http-equiv=\"Refresh\" content=\"1;URL=/$clientFolderName/$projectFolderNameindex.php?" .
"section=$section&action=" . $redirectArray[$section][$myImmedAction] . '&success=' . urlencode($this->getSuccessMsg());
$html .= ($additionalQS) ? '&' . urlencode($additionalQS) : '';
$html .= "\">\n</noscript>\n\n";
}
return $html;
}
function &getSuccessMsg() { // STATIC STRING METHOD
global $action, $choice, $db;
return 'Successful ' . strtoupper($action) . ' ' . strtoupper($choice) . " to database $db - <a href=index.php?willViewLog=1>View Log</a><p>";
}
function setErrorArray($additionalErrorArray) { // VOID METHOD
if (is_array($this->errorArray) && @sizeof($this->errorArray) > 0 && is_array($additionalErrorArray) && @sizeof($additionalErrorArray) > 0) {
$this->errorArray =& $this->errorArray + $additionalErrorArray;
} elseif (is_array($additionalErrorArray) && @sizeof($additionalErrorArray) > 0) {
$this->errorArray =& $additionalErrorArray;
}
}
//--------------------------------------------------* END OF GETTER/SETTER METHODS *-------------------------------------------------------------------------
function &convert_to_HTML($content) { // STATIC HTML STRING METHOD
$content = preg_replace('/[\n\r]/i', '<BR>', $content);
$content = preg_replace('/[\s]/i', ' ', $content);
return $content;
}
function &convert_to_text($content) { // STATIC TEXT STRING METHOD
$content = preg_replace('/<br[^>]*>/i', '\n', $content);
$content = preg_replace('/ /i', '\s', $content);
return $content;
}
}
THAT is my entire supposed Singleton class in its actual entirety. It is set up that way to call static methods upon a static array. That way, whenever you have this call in your class:
if (somethingsWrong) ActionHandler::setErrorArray('action' => 'Something is wrong'));
However, consider this example: I call an instance of a FileGenerator class from my ActionPerformer->perform() method. FileGenerator class object's getBackupFile() method does some code and if it breaks:
ActionHandler::setErrorArray('action' => 'Something is wrong'));
Everything in the FileGenerator class object method works just fine and were I to do this:
print_r(ActionHandler::getErrorArray());
I see:
Array (action => Something is wrong)
But the moment I leave the getBackupFile() method from FileGenerator class object and return to the code in ActionPerformer->perform() method and were I do this:
print_r(ActionHandler::getErrorArray());
I see:
How then in PHP do you assure that anytime I call ActionHandler::setErrorArray() that the error array, which is supposed to be static, remains a single-instance entity with a value available to ALL classes everywhere? How do you build a singleton class in PHP?
Phil