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:46:17 EST 2004


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 ());
>>
>
>
> _______________________________________________
> 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:54:15 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 6A78FA86CD
	for <talk at lists.nyphp.org>; Thu, 12 Feb 2004 15:54:15 -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] Array handling - Why does this happen?
Date: Thu, 12 Feb 2004 12:54:12 -0800
Message-ID: <41EE526EC2D3C74286415780D3BA9F87772B97 at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
Thread-Topic: [nycphp-talk] Array handling - Why does this happen?
Thread-Index: AcPxjdx1MqtyXCS/RTWqlbgyXem2EAAGyhqw
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:54:15 -0000


> However for this array,
>=20
> $question['1']=3Darray('type'=3D>'radio',
>                      'showresults'=3D>1,
>                      'required'=3D>0,
>                      'question'=3D>'Does your community....',
>                      'answer'=3D>array('Yes'=3D>'Yes','No'=3D>'No'));
>
> $q_id=3D1;
> $val=3D'Yes';
> $val1=3D'No';
>=20
> $question[$q_id]['answer'][$val]['count']=3D8888;

For one, $q_id is an integer, while the '1' element of $question['1'] is
declared as a string.  Thus, $question[$q_id] has a cast involved, and
it might not be clear how it's being casted, thus resulting in a
associative or indexed array.  Probably not the problem, but if you're
using a number, keep it without quotes.

> echo $question[$q_id]['answer'][$val]['count']."<br>";
>=20
> $question[$q_id]['answer'][$val1]['count']=3D9999;
>=20
> echo $question[$q_id]['answer'][$val1]['count']."<br>";
>=20
> will echo back 'count' as being 8 and 9

There's a strange cast happening.  When you declare the answer element,
it's being declared as an associative array, containing and Yes and No
elements.  These Yes and No elements contain strings - not arrays.  So,

$question[$q_id]['answer'][$val]['count']=3D8888

is trying to reference the element named by $val (in this case Yes) as
an array, when it's a string.  You'd need to declare the 'answer'
element something like:

'answer'=3D>array('Yes'=3D>array('Yes',0),'No'=3D>array('No',0));

Then

$question[$q_id]['answer'][$val]['count']=3D8888;

should work as expected.  All this said, I'd rethink using this level of
multidimensional arrays  :)

H





More information about the talk mailing list