On Dec 21, 2007 2:46 PM, Robert Cummings <[EMAIL PROTECTED]> wrote:

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


the global space does have good uses; i prefer to use it to cache on a
per-request
basis.  i think the global space can be easily abused however, as is the
case in
many projects ive encountered and unfortunately work on.
in many cases singleton is an excellent alternative, however, it add
complexity and
overhead, so sometimes globals are appropriate.

-nathan

Reply via email to