[UPHPU] $GLOBALS and global best practices

Joseph Scott joseph at josephscott.org
Mon Nov 7 15:05:32 MST 2011

On Mon, Nov 7, 2011 at 11:16 AM, Mac Newbold <mac at macnewbold.com> wrote:
> The idea of using a singleton object is sounding nice to me, but I'm
> wondering how one avoids having to either pass it around everywhere,
> or reinstantiate the object (even though you're going to get back the
> same singleton reference) everywhere you want to use it.

I've fought this fight multiple times.  For people who want to avoid
global variables at all costs they end up implementing global classes
essentially (or pass data in and out like mad), sometimes with some
combination of constants.

In most PHP apps I've resigned myself to having a just a few global
variables, the fewer the better.  And what I'd really like is a way to
define a constant array, since I prefer to have virtually all global
items to be read only.

The more important process is the analysis of why you are or aren't
going to use global variables.  Each approach has trade offs, you have
to decide which trade offs you want to deal with.

Joseph Scott
joseph at josephscott.org

More information about the UPHPU mailing list