[nycphp-talk] How to create a singleton class in PHP
Dan Cech
dcech at phpwerx.net
Thu Feb 12 14:32:10 EST 2004
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 ());
Phil Powell wrote:
> This is what I wrote based on your code:
>
> class ActionHandler {
>
> function &ActionHandler() {} // LEAVE IT EMPTY
>
> function &getErrorArray() {
> return ActionHandler::setErrorArray();
> }
>
> function &setErrorArray($additionalErrorArray = null) {
> static $errorArray = array();
> if (is_array($additionalErrorArray)) $errorArray +=
> $additionalErrorArray;
> return $errorArray;
> }
>
> }
>
> so basically I am assuming that ActionHandler does not create a static
> instance of $this->errorArray but instead ActionHandler::setErrorArray()
> creates a static instance of a localized $errorArray which, since only
> one instance of ActionHandler exists, only one instance of the method
> exists, then only one instance of the localized variable exists and and
> that is that. Am I off-base?
>
> Phil
>
> Dan Cech wrote:
>
>> Ok, now we have something that works! Which one works?
>>
>> Both should function just fine, in each case there should only ever be
>> one instance of the $errorArray variable, whether it is a static or
>> global.
>>
>> The reason the code is much simpler than what you had previously is
>> that it is not designed to try and store an instance of ActionHandler,
>> but only an instance of the errorArray variable.
>>
>> Dan
>>
>> Phil Powell wrote:
>>
>>> Dan Cech wrote:
>>>
>>>> Hmm, that is very similar to the GLOBALS kludge I posted, although
>>>> it doesn't work on my machine. Each subsequent call to
>>>> setErrorArray is overwriting the error array.
>>>>
>>>> Thus far the best I've got is:
>>>>
>>>> class ActionHandler {
>>>> function &getErrorArray () {
>>>> return ActionHandler::setErrorArray ();
>>>> }
>>>>
>>>> function &setErrorArray ($additionalErrorArray = NULL) {
>>>> static $errorArray = array ();
>>>>
>>>> if ( is_array ($additionalErrorArray) ) {
>>>> $errorArray += $additionalErrorArray;
>>>> }
>>>>
>>>> return $errorArray;
>>>> }
>>>> }
>>>>
>>>> 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
>>
>
>
More information about the talk
mailing list