Re: Opinion about the using $GLOBALS directly

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2007-12-21 at 12:35 -0600, Philip Thompson wrote:
> On Dec 20, 2007, at 11:24 AM, Sancar Saran wrote:
> 
> > Hello All,
> >
> > Thanks for joining the conversation. It seems there where no real  
> > technical
> > dead end for using $GLOBALS directly.
> > Using $GLOBALS directly very similar to coding standarts. It just up  
> > to you.
> >
> > Also I try explain my enviroment a liddle bit.
> >
> > First of all my function declarationgs gonna like this
> >
> > // Set Shorcut Variables
> > $arrConf   = &$GLOBALS['_LIVE']['_CONF'];
> > $arrStat   = &$GLOBALS['_LIVE']['_STAT'];
> > $arrDomSet = &$GLOBALS['_LIVE']['_DOMN'][$GLOBALS['_LIVE']['_STAT'] 
> > ['curDom']]
> > ['settings'];
> > $arrLang   = &$GLOBALS['_LIVE']['_LANG'][$arrStat['language']];
> > $rootDir   = &$arrConf['rootDir'];
> > $webDir    = &$arrConf['webDir'];
> > $arrDb     = &$arrConf['_DB'];
> > $arrDbg    = &$GLOBALS['_LIVE']['_DEBG'];
> 
> Why are you using references to the $GLOBALS? If they're global, then  
> there's only 1 instance/reference to it. Seems needless...

Here's why:

<?php

$GLOBALS['myProject']['foo'] = 10;

function aFunction()
{
    $foo = $GLOBALS['myProject']['foo'];

    echo "Foo: $foo\n";
    $foo--;
}

function anotherFunction()
{
    $foo = $GLOBALS['myProject']['foo'];

    echo "Foo: $foo\n";
    $foo--;
}

aFunction();
anotherFunction();

print_r( $GLOBALS['myProject'] );

?>

When in fact, almost assuredly the following was desired:

<?php

$GLOBALS['myProject']['foo'] = 10;

function aFunction()
{
    $foo = &$GLOBALS['myProject']['foo'];

    echo "Foo: $foo\n";
    $foo--;
}

function anotherFunction()
{
    $foo = &$GLOBALS['myProject']['foo'];

    echo "Foo: $foo\n";
    $foo--;
}

aFunction();
anotherFunction();

print_r( $GLOBALS['myProject'] );

?>

Personally, I think $GLOBALS is a great location for project
configurations but I think it's necessary to use a second level for the
configuration and first level defines a space for your project. In this
way you're much less likely to run into variable clobbering issues. This
is why I used 'myProject' as the first level key in the above example.
Some purists out there will misguidedly say that nothing belongs in the
global space; however, they are wrong since function and class
definitions fall into global space and as such the solution is
equivalent to stuffing all your config vars into a class or function.
That said, I think use of the global space shouldn't be ad-hoc and
sloppy.

Cheers,
Rob.
-- 
...........................................................
SwarmBuy.com - http://www.swarmbuy.com

    Leveraging the buying power of the masses!
...........................................................

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux