Re: Using Static Class Variables to Access Globally

From: Date: Wed, 24 Sep 2008 12:07:19 +0000
Subject: Re: Using Static Class Variables to Access Globally
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-280834@lists.php.net to get a copy of this message
On Tue, Sep 23, 2008 at 9:03 PM, Ryan Panning <rpanning@gmail.com> wrote: > The typical way to access a variable or instance from inside a > function/method is to either declare it a global variable or pass it as a > argument. Is there any reason why someone shouldn't use static class > variables to do this? Ex: > > <?php > class Foo { > public static $bar_instance; > } > > class Bar { > public function do_something() {} > } > > Foo::$bar_instance = new Bar; > > function foo_bar() { > Foo::$bar_instance->do_something(); > } > > foo_bar(); > ?> > > Crude example but imagine this on a larger scale. I'm thinking there may be > some kind of php optimization that this would hamper or something to that > effect. > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > You might also look into these concepts: - service locator - registry pattern - dependency injection I wouldn't create something like what you're doing without a very good documented reason for it. The reason everything is hidden behind methods/structures is so that you can change your code and have a layer to deal with the changes. By exposing a variable to be public static then you're opening your implementation up allowing people to rely on it forcing you to be stuck in a rut if you need to swap it out for something else. Blah blah blah. ;)

« previous php.general (#280834) next »