Re: tired of referencing static variables/constants with 'self ::'

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

 



On Tue, May 9, 2006 12:42 pm, D. Dante Lorenso wrote:
> Does anyone know if it's possible to reference class constants or
> static
> variables without having to use 'self::' all the time?
>
> class A {
>     const MY_CONSTANT = true;
>
>     public function test() {
>        echo self :: MY_CONSTANT; // works
>        echo MY_CONSTANT; // doesn't work
>     }
> }
>
> I don't think it is possible, but why not?  Can zend/php make it work?
> Seems it could since this works:
>
> define('MY_CONSTANT', true);
> class A {
>     public function test() {
>        echo MY_CONSTANT; // works
>     }
> }
>
> It's just annoying my how much boiler-plate I have to write all the
> time.

#1.
You might do:
define('MY_CONSTANT', true);
class A {
  const MY_CONSTANT = MY_CONSTANT;
}

Then you can sort of have the best of both worlds...
Though in a hack sort of way. :-^

#2.
If it's that boiler-plate, maybe your class design is flawed...

I mean, the whole point of all this OOP stuff is re-factoring out the
commonalities of code to get a high level of code re-use.

If you're writing something so boiler-plate that it drives you crazy,
maybe you need to step back and look at the whole thing "sideways" and
see if your "obvious" class hierarchy isn't really the best hierarchy
to have chosen.

-- 
Like Music?
http://l-i-e.com/artists.htm

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