Re: nested, referenced foreach & implicit current array pointer issues

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

 



On Thu, 2007-02-01 at 16:42 +0100, Roman Neuhauser wrote:
>
> If PHP was statically typed, global variables would still be a bad
> smell.  They are bad smell in C++ and Java, for example.  It's too easy
> to call getfoo() before you have set up $foo.  The risk grows
> exponentially: as soon as you add another global, $bar, you risk that
> you or someone else will use getfoo() inside initbar(), and getbar()
> inside initfoo() (or getfoo() inside initfoo()).  Of course, it will be
> several function calls deep, and quite probably only happen in a code
> path that's rarly used (such as error handling).

Nopthing wrong with globals as long as they aren't used to punt data
around from function to function. I find globals quite useful when used
for configuration. I usually use a double level array. The first index
is a grouping index such as "someProject" the second index is the name
of the property. I could use a database table, but why incur an extra
query. I could use a class, but why increase complexity, I could use
functions, but complexity again. As for singletons... just use a static
class method.

<?php

class Foo extends Singleton
{
    function Foo()
    {
        static $createdAlready = false;

        if( $createdAlready )
        {
            die( 'Use Foo::getGlobalInstance() instead.' );
        }

        $createdAlready = true;
    }

    function getGlobalInstance()
    {
        static $singleton = null;

        if( $singleton === null )
        {
            $singleton = &new Foo();
        }

        return $singleton;
    }
}

?>

Now how hard was that!?

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

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