Re: Using Static Class Variables to Access Globally

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

 



On Tue, Sep 23, 2008 at 7:03 PM, Ryan Panning <rpanning@xxxxxxxxx> 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.
>

in many cases, people like to drive client code through methods, which,
given the current set of language features in php, could be reason to favor
a singleton w/ __get() & __set() methods defined.  you still have the same
'global' scope, except that the data doesnt have to be public.  (im not
saying its bad to use public vars, im merely presenting an alternative
perspective).

supposedly php has in the rfc about static classes to add __setStatic() &
__getStatic(), but support isnt there yet, and im starting to doubt it will
be available for 5.3 =/

-nathan

[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