[nycphp-talk] How to create a singleton class in PHP
Phil Powell
phillip.powell at adnet-sys.com
Thu Feb 12 15:55:51 EST 2004
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 ());
>>>
>>>
>>
>>
>> _______________________________________________
>> 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 16:00:07 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 AEB7AA86CD
for <talk at lists.nyphp.org>; Thu, 12 Feb 2004 16:00:06 -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] mysql pro cert (anyone get tested?)
Date: Thu, 12 Feb 2004 13:00:04 -0800
Message-ID: <41EE526EC2D3C74286415780D3BA9F87772B9F at ehost011-1.exch011.intermedia.net>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Thread-Topic: [nycphp-talk] mysql pro cert (anyone get tested?)
Thread-Index: AcPxijke17w5UYMuRPGlyAgEM6NLFQAIRb3w
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 21:00:07 -0000
> has anyone here gone for the official mysql certification?
>=20
> any feedback on the tests themselves? i checked the website and they
had
> advanced training coming to nyc in march and now its not there, i find
it
> kinda odd that there would be no interest in nyc ... anyone here
thought
> about teaching/giving advanced mysql class? there is no ramp on it,
had it
> been suggested?
MySQL training will be coming soon, and in fact, we plan to have a
presentation on it in the spring.
Stay tuned.... :)
H
More information about the talk
mailing list