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 13:10:53 EST 2004


jon baer wrote:

>racking my brain ... besides being cleaner why did your class work?  i didnt
>see anything wrong w/ what he did (but it didn't work on multiple objects)
>...
>
>- jon
>
>----- Original Message -----
>From: "Dan Cech" <dcech at phpwerx.net>
>To: "NYPHP Talk" <talk at lists.nyphp.org>
>Sent: Thursday, February 12, 2004 12:34 PM
>Subject: Re: [nycphp-talk] How to create a singleton class in PHP
>
>
>  
>
>>What you want is:
>>
>>class ActionHandler {
>>   function &getErrorArray () {
>>     return ActionHandler::setErrorArray ();
>>   }
>>    
>>
>
>_______________________________________________
>talk mailing list
>talk at lists.nyphp.org
>http://lists.nyphp.org/mailman/listinfo/talk
>
>  
>
I found a rather tragic kluge to get my code working again:

class ActionHandler {

 function &ActionHandler() {
  static $errorArray = array();
 }

 function &getErrorArray() {
  return $GLOBALS[$errorArray];
 }

 function &setErrorArray($additionalErrorArray) {
  if (is_array($additionalErrorArray)) $this->errorArray =& 
array_merge($this->errorArray, $additionalErrorArray);
  if (!isset($GLOBALS[$errorArray])) $GLOBALS[$errorArray] = 
$this->errorArray;
 }

}

That produces the results I want every time, and I can't understand why 
I must use $GLOBALS[$errorArray] and not $GLOBALS['errorArray'], am I 
not mistaken that arrays can only take scalar string and/or integer 
values as keys?

Phil

-- 
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