[nycphp-talk] How to create a singleton class in PHP
Phil Powell
phillip.powell at adnet-sys.com
Thu Feb 12 16:31:29 EST 2004
The keys are supposed to be unique, but I can work on that a bit later.
Here is the most simplistic rendition of my code (using your code
changes verbatim!!) and the results:
class ActionHandler {
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------
This class will be a static class structure. Is primarly used to reference a static instance of setErrorArray() method which instantiates
a single instance of a localized $errorArray variable. Since the localized variable is called as static only one instance of it will exist;
since the method called by the class is referenced (prefixed with '&'), and since the class itself is called as "static" reference by
avoiding use of a constructor to ensure it is not meant to be instantiated, only one $errorArray will ever exist.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
// NO CONSTRUCTOR NECESSARY
//----------------------------------------------------* GETTER/SETTER METHODS *--------------------------------------------------------------
function &getErrorArray() { // STATIC ARRAY METHOD
return ActionHandler::_ErrorArray();
}
function &setErrorArray($additionalErrorArray = NULL) { // "VOID" METHOD (THROWS AN ARRAY CAUGHT BY getErrorArray())
return ActionHandler::_ErrorArray($additionalErrorArray);
}
function &_ErrorArray($additionalErrorArray = NULL) {
static $errorArray = array();
print_r("additionalErrorArray = "); print_r($additionalErrorArray); print_r(" is array? "); print_r(is_array($additionalErrorArray)); print_r("<P>");
if (is_array($additionalErrorArray)) $errorArray += $additionalErrorArray;
print_r("errorArray = "); print_r($errorArray); print_r("<P>");
return $errorArray;
}
//--------------------------------------------------* END OF GETTER/SETTER METHODS *-------------------------------------------------------------------------
}
class FileGenerator {
var $db, $table, $action, $choice, $fileName, $file ; // PROPERTIES
function FileGenerator($db, $table, $action, $choice) { // CONSTRUCTOR
foreach (array('db', 'table', 'action', 'choice') as $val) $this->$val = ${$val};
}
//------------------------------ --* GETTER/SETTER METHODS *-- -------------------------------------------
function getBackupFile($db) { // STRING METHOD
/*-----------------------------------------------------------
This will obtain the specific backup file only
-------------------------------------------------------------*/
global $_POST, $backupFilePath;
$postExemptionArray = array('db');
if (is_array($_POST) && @sizeof($_POST) > 0) {
foreach ($_POST as $key => $val)
if (!isset(${$key}) && !in_array($key, $postExemptionArray)) ${$key} = $val; // SET ALL VARS EXCEPT THOSE WHOSE NAMES ARE IN EXEMPTION ARRAY
}
$fileID = @fopen("$backupFilePath/$db/" . $this->table . "/${file_year}-${file_month}-${file_day}_" . strtoupper($choice) . '.sql', 'r');
if (!$fileID) {
ActionHandler::setErrorArray(array('willSelectFileArray' =>
"Could not locate backup file /$db/$table/${file_year}-${file_month}-${file_day}_" . strtoupper($choice) . '.sql')
);
return null;
}
$contents = @fread($fileID, filesize("$backupFilePath/$db/$table/${file_year}-${file_month}-${file_day}_" . strtoupper($choice) . '.sql'));
@fclose($fileID);
return $contents;
}
}
class ActionPerformer {
/*---------------------------------------------------------------------------------------------------------------------------------------------
ActionPerformer extends abstract class DBAction which, in turn, extends MethodGeneratorForActionPerformer.
Not very OO but it works. Used to override abstract method perform() necessary for all action classes
as well as the getter/setter methods in the grandparent class to use for error handling, redirection, etc.
Will either backup or restore database depending on value of globalized $action variable
----------------------------------------------------------------------------------------------------------------------------------------------*/
function ActionPerformer() {} // CONSTRUCTOR
function perform() { // VOID METHOD
$db = 'phil'; $originalDB = 'oldphil';
$tables = array('table1', 'table2', 'table3', 'table4');
$action = 'restore'; $choice = 'full';
foreach ($tables as $table) {
$fileGen =& new FileGenerator($db, $table, $action, $choice);
$fileContents = $fileGen->getBackupFile($originalDB);
}
}
}
OUTPUT:
additionalErrorArray = is array?
errorArray = Array ( )
additionalErrorArray = Array ( ) is array? 1
errorArray = Array ( )
additionalErrorArray = is array?
errorArray = Array ( )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/event/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/image/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/image_event_assoc/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/image_keyword_assoc/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/image_person_assoc/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/image_placement_assoc/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/keyword/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/person/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/person_event_assoc/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/placement/2004-01-01_FULL.sql ) is array? 1
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
additionalErrorArray = is array?
errorArray = Array ( [willSelectFileArray] => Could not locate backup file /dev/department/2004-01-01_FULL.sql )
Dan Cech wrote:
> You have a typo in the sizeof call.
>
> In addition, the reason you only ever get one entry in the $errorArray
> is that each key must be unique, otherwise you are just going to
> overwrite the old key.
>
> I think you may want to use:
>
> $errorArray[] = $additionalErrorArray;
>
> instead of:
>
> $errorArray += $additionalErrorArray;
>
> or make the keys unique.
>
> Dan
>
> Phil Powell wrote:
>
>> Got it.. well, not the code working but I can copy and paste now (why
>> I couldn't before I have no idea).. see below for your code and its
>> results.. it's mixed. I do populate $GLOBALS['errorArray'], but only
>> once, in spite of $additionalErrorArray value which setErrorArray()
>> still thinks is NOT an array even though it is!
>>
>> Phil
>>
>>>>> and for the $GLOBALS kludge:
>>>>>
>>>>> class ActionHandler {
>>>>> function &getErrorArray () {
>>>>> return ActionHandler::setErrorArray ();
>>>>> }
>>>>>
>>>>> function &setErrorArray ($additionalErrorArray = NULL) {
>>>>> if ( !isset ($GLOBALS['errorArray']) ) {
>>>>> $GLOBALS['errorArray'] = array ();
>>>>> }
>>>>>
>>>>> if ( is_array ($additionalErrorArray) ) {
>>>>> $GLOBALS['errorArray'] += $additionalErrorArray;
>>>>> }
>>>>>
>>>>> return $GLOBALS['errorArray'];
>>>>> }
>>>>> }
>>>>>
>>>>> Both of those functions work 100% for anything I've thrown at them.
>>>>
>>>>
>>>>
>>>>
>>>> It works on mine as well. How it works is a mystery to me, how is
>>>> it working?
>>>>
>>>> Phil
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> talk mailing list
>>> talk at lists.nyphp.org
>>> http://lists.nyphp.org/mailman/listinfo/talk
>>>
>> function &setErrorArray($additionalErrorArray = '')
>> { // "VOID" METHOD (THROWS AN ARRAY CAUGHT
>> BY getErrorArray())
>> /*
>> static $errorArray = array();
>>
>> if (is_array($additionalErrorArray)) $errorArray =
>> $errorArray + $additionalErrorArray;
>>
>> return $errorArray;
>> */
>> print_r("additionalErrorArray = ");
>> print_r($additionalErrorArray); print_r(" is array? ");
>> print_r(sizeof($addtionalErrorArray)); print_r("<P>");
>> if (!isset($GLOBALS['errorArray'])) $GLOBALS['errorArray'] =
>> array();
>> if (is_array($additionalErrorArray)) $GLOBALS['errorArray']
>> += $additionalErrorArray;
>> print_r("GLOBALS['errorArray'] = ");
>> print_r($GLOBALS['errorArray']); print_r("<P>");
>> return $GLOBALS['errorArray'];
>> }
>>
>> function &getErrorArray() {
>> return ActionHandler::setErrorArray();
>> }
>>
>> OUTPUT
>>
>> additionalErrorArray = Array ( ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/event/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/image/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/image_event_assoc/2004-01-01_FULL.sql ) is
>> array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/image_keyword_assoc/2004-01-01_FULL.sql ) is
>> array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/image_person_assoc/2004-01-01_FULL.sql ) is
>> array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/image_placement_assoc/2004-01-01_FULL.sql )
>> is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/keyword/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/person/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/person_event_assoc/2004-01-01_FULL.sql ) is
>> array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>> additionalErrorArray = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/placement/2004-01-01_FULL.sql ) is array? 0
>>
>> GLOBALS['errorArray'] = Array ( [willSelectFileArray] => Could not
>> locate backup file /dev/department/2004-01-01_FULL.sql )
>>
>>
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>
--
Phil Powell
Web Developer
ADNET Systems, Inc.
11260 Roger Bacon Drive, Suite 403
Reston, VA 20190-5203
Phone: (703) 709-7218 x107 Cell: (571) 437-4430 FAX: (703) 709-7219
EMail: Phillip.Powell at adnet-sys.com AOL IM: SOA Dude
More information about the talk
mailing list