NYCPHP Meetup

NYPHP.org

[nycphp-talk] How to create a singleton class in PHP

Phil Powell phillip.powell at adnet-sys.com
Thu Feb 12 15:27:50 EST 2004


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 ());
>
> 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
>>>
>>
>>
>
>
> _______________________________________________
> 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






>From hans not junk at nyphp.com  Thu Feb 12 15:39:08 2004
Return-Path: <hans not junk at nyphp.com>
Received: from ehost011-1.exch011.intermedia.net (unknown [64.78.21.3])
	by virtu.nyphp.org (Postfix) with ESMTP id B6838A86CD
	for <talk at lists.nyphp.org>; Thu, 12 Feb 2004 15:39:07 -0500 (EST)
X-MimeOLE: Produced By Microsoft Exchange V6.5.6944.0
Content-class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Subject: RE: [nycphp-talk] Translating XML to SQL
Date: Thu, 12 Feb 2004 12:39:05 -0800
Message-ID: <41EE526EC2D3C74286415780D3BA9F87772B8C at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
Thread-Topic: [nycphp-talk] Translating XML to SQL
Thread-Index: AcPxfZzTBjfEOh2pTBuO86MacxiCCQAKpu2w
From: "Hans Zaunere" <hans not junk at nyphp.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
X-BeenThere: talk at lists.nyphp.org
X-Mailman-Version: 2.1.2
Precedence: list
Reply-To: NYPHP Talk <talk at lists.nyphp.org>
List-Id: NYPHP Talk  <talk.lists.nyphp.org>
List-Unsubscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
	<mailto:talk-request at lists.nyphp.org?subject=unsubscribe>
List-Archive: <http://lists.nyphp.org/pipermail/talk>
List-Post: <mailto:talk at lists.nyphp.org>
List-Help: <mailto:talk-request at lists.nyphp.org?subject=help>
List-Subscribe: <http://lists.nyphp.org/mailman/listinfo/talk>,
	<mailto:talk-request at lists.nyphp.org?subject=subscribe>
X-List-Received-Date: Thu, 12 Feb 2004 20:39:08 -0000


> I'll consider the article idea. At this point, I've done enough head
> scratching that I think it'd be nice to save anyone else the grief.
I'll
> work on collecting and writing up my experiences.

Great... it's off to a great start already, and a very interesting
topic, with others interested, too.  Let me know how things go.

Thanks Eric,

H




More information about the talk mailing list