[nycphp-talk] How to create a singleton class in PHP
Dan Cech
dcech at phpwerx.net
Thu Feb 12 16:08:48 EST 2004
Phil,
The code you posted below is not going to work, you are mixing the
static and global approaches, which I don't see the point of at all.
I am happy to help you with this problem, however I don't see what I can
do if you modify my suggested code and as a consequence it breaks.
Please try the following code without modification and let me know your
results. If it does not work, please also attempt to reduce your code
to the minimum required to duplicate the error. The reason I ask this
is so that I can actually run the code on my machine and determine
whether it is the code which is causing the error, or a problem with php
on your server.
class ActionHandler {
function &getErrorArray () {
return ActionHandler::_ErrorArray ();
}
function &setErrorArray ($additionalErrorArray = NULL) {
return ActionHandler::_ErrorArray ($additionalErrorArray);
}
function &_ErrorArray ($additionalErrorArray = NULL) {
static $errorArray = array ();
if ( is_array ($additionalErrorArray) ) {
$errorArray += $additionalErrorArray;
}
return $errorArray;
}
}
Dan
Phil Powell wrote:
> 100% failure on $GLOBALS, even $GLOBALS['errorArray'] never even
> CREATED!!!!
>
> function &setErrorArray($additionalErrorArray = '') {
> static $errorArray = array();
> print_r("additionalErrorArray = "); print_r($additionalErrorArray);
> print_r(" is array? "); print_r(sizeof($additionalErrorArray));
> print_r("<P>");
> if (is_array($additionalErrorArray)) $errorArray += $additionalErrorArray;
> print_r("errorArray = "); print_r($errorArray); print_r("<P>");
> if (!isset($GLOBALS['errorArray'])) $GLOBALS['errorArray'] = $errorArray;
> print_r("GLOBALS['errorArray'] = "); print_r($GLOBALS['errorArray']);
> print_r("<P>");
> }
>
> Here is a sample of the output:
>
> additionalErrorArray = Array ( [willSelectFileArray] => Could not locate
> backup file ) is array? 0
> errorArray = Array ( [willSelectFileArray] => Could not locate backup file)
> GLOBALS['errorArray'] = Array ( )
>
> Phil
>
> Phil Powell wrote:
>
>> I'm using PHP 4.3.2 on the development server box here at work. Sorry
>> about the mistakes, I had to copy over the code by hand as there are
>> firewall issues right now between the boxes, so I can't just easily
>> punt the code over, but it's pretty much verbatim. I'll try the
>> $GLOBALS code instead.
>>
>> Phil
>>
>> Dan Cech wrote:
>>
>>> What version of PHP are you using Phil, also does the code I provided
>>> in my last email (below) work without modification?
>>>
>>> The code you posted below is actually missing a few closing brackets,
>>> but other than that, adding a line to print details of
>>> $additionalErrorArray does not cause any issues for me.
>>>
>>> Dan
>>>
>>> Phil Powell wrote:
>>>
>>>> Ran into a very weird problem now:
>>>>
>>>> function &setErrorArray($additionalErrorArray) {
>>>> static $errorArray = array();
>>>> print_r("additionalErrorArray: "); print_r($additionalErrorArray);
>>>> print_r(" is array? "); print_r(sizeof($additionalErrorArray);
>>>> print_r("<P>");
>>>> if (is_array($additionalErrorArray) $errorArray +=
>>>> $additionalErrorArray;
>>>> return $errorArray;
>>>> }
>>>>
>>>> If I happen to actually be passing an array into setErrorArray here
>>>> is my output using:
>>>>
>>>> ActionHandler::setErrorArray(array('willSelectFileArray' => 'Could
>>>> not locate backup file'));
>>>>
>>>> Output:
>>>>
>>>> additionalErrorArray: Array ( [willSelectFileArray] => Could not
>>>> locate backup file ) is array? 0
>>>>
>>>> For some reason $additionalErrorArray lists as an array but has a
>>>> "sizeof" of 0. If I use is_array() instead of sizeof() I get
>>>> false. In short, the static method assumes the parameter is not an
>>>> array, even though it is, thus, the entire method fails to add any
>>>> additional items onto the static var $errorArray because it thinks
>>>> everything it gets is not an array, even though.. it is.
>>>>
>>>> Phil
>>>>
>>>> Dan Cech wrote:
>>>>
>>>>> You shouldn't need the constructor, because the methods are only
>>>>> ever called as class methods, so it will never be executed.
>>>>>
>>>>> But you are absolutely correct about the fact that that there is
>>>>> only ever one instance of the $errorArray created within the
>>>>> setErrorArray function.
>>>>>
>>>>> Dan
>>>>>
>>>>> PS, if you wanted to be able to extend this class, you could use:
>>>>>
>>>>> class ActionHandler {
>>>>> function &getErrorArray() {
>>>>> return ActionHandler::_ErrorArray();
>>>>> }
>>>>>
>>>>> function &setErrorArray($additionalErrorArray = null) {
>>>>> return ActionHandler::_ErrorArray($additionalErrorArray);
>>>>> }
>>>>>
>>>>> function &_ErrorArray ($additionalErrorArray = null) {
>>>>> static $errorArray = array();
>>>>> if (is_array($additionalErrorArray)) {
>>>>> $errorArray += $additionalErrorArray;
>>>>> }
>>>>> return $errorArray;
>>>>> }
>>>>> }
>>>>>
>>>>> You could then do:
>>>>>
>>>>> class myclass extends ActionHandler {
>>>>> }
>>>>>
>>>>> $myobj = new myclass ();
>>>>> $myobj->setErrorArray (array ('test' => 'test'));
>>>>>
>>>>> $myobj2 = new myclass ();
>>>>> $myobj2->setErrorArray (array('hello' => 'tiger'));
>>>>>
>>>>> print_r (ActionHandler::getErrorArray ());
More information about the talk
mailing list