[UPHPU] $GLOBALS and global best practices

Mac Newbold mac at macnewbold.com
Mon Nov 7 11:16:46 MST 2011

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.

For example, take global configuration data. It would be nice to have
something like $conf->url accessible everywhere, but to get $conf, I
either have to do $conf=new Config(), assuming Config is a singleton
class, or pass in $conf when I create any object, catch it in the
constructor and set it to a class variable, then use $this->conf->url
everywhere instead.

Am I missing something? Is there a good way to get around either of those?

The idea of using static class methods kind of appeals to me, so I can
use Conf::url() anywhere without having to pass around variables or
instantiate a Config class all over. Using a magic method like
__call() the Conf class could even be set up so that it returns the
value of the corresponding class variable when an unknown function is
called, so you wouldn't have to set up a function for every config
variable you wanted to be able to retrieve.


On Mon, Nov 7, 2011 at 10:59 AM, Michael <romanovic at romanovic.com> wrote:
> On 11/7/2011 10:39, Wade Preston Shearer wrote:
>> What are the groups thoughts on use of the $GLOBALS superglobal and using the global command to pull variables into scope? Are there times when using these are considered good practice or would good architecture always provide an alternate solution (such as passing the variable into the function)?
> I've almost always been able to avoid globals when writing
> object-oriented web applications.  However, it was a question I also
> contemplated recently when trying to decide the best way to make my
> application configuration settings globally accessible (so I'd be
> interested to hear people's preferred method on that as well).  The
> singleton approach that Walt mentioned seems the most appropriate to me,
> so far.
> What situation are you considering using globals for?
> Mike
> _______________________________________________
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net

Mac Newbold
mac at macnewbold.com

More information about the UPHPU mailing list