Constant Scope PHP5

php.internals

Arjen Brouwer

23 years ago
Should'nt this work? Currently it reports an error about a undefined constant. <? class Test { const FOO = 100; var $bar = FOO; } $Test = new Test; echo $Test->bar; ?> Ciao, Arjen

Cristiano Duarte

23 years ago
"Arjen Brouwer" <arjenjb@quicknet.nl> escreveu na mensagem news:001b01c368a1$775b2070$6402a8c0@sleepert...
> Should'nt this work? Currently it reports an error about a undefined
constant.
> > <? > class Test { > const FOO = 100; > var $bar = FOO; > } > > $Test = new Test; > echo $Test->bar; > ?> > > Ciao, > Arjen >
IMHO it should. Cristiano Duarte.

Zeev Suraski

23 years ago
At 14:35 22/08/2003, Arjen Brouwer wrote:
>Should'nt this work? Currently it reports an error about a undefined constant. > ><? >class Test { > const FOO = 100; > var $bar = FOO;
Should be var $bar = Test::FOO (or self::FOO, even though it appears not to work right now) - there's no implicit class/object dereference of any kind in PHP. Zeev

DvDmanDT

23 years ago
Wait a sec... When did const become a keyword? And since when can you initialize variables from outside a constructor/function? Sorry to bother..
-- // DvDmanDT MSN: dvdmandt@hotmail.com Mail: dvdmandt@telia.com "Zeev Suraski" <zeev@zend.com> skrev i meddelandet news:5.1.0.14.2.20030823012845.0576f768@localhost... > At 14:35 22/08/2003, Arjen Brouwer wrote: > >Should'nt this work? Currently it reports an error about a undefined constant. > > > ><? > >class Test { > > const FOO = 100; > > var $bar = FOO; > > Should be var $bar = Test::FOO (or self::FOO, even though it appears not to

Zeev Suraski

23 years ago
At 07:03 23/08/2003, DvDmanDT wrote:
>Wait a sec... When did const become a keyword?
In PHP 5.0.
>And since when can you >initialize variables from outside a constructor/function?
Since PHP 3.0 :) Zeev

l0t3k

23 years ago
Zeev, speaking of const, i have a feature request. why not accept "const" in the global context and create a compile-time global constant ? e.g. const FOO = 1 ; const BAR = 2; class Barbara { const Barbara::BUSH = FOO; }; i'd much prefer this as an alternative to the runtime define(). l0t3k "Zeev Suraski" <zeev@zend.com> wrote in message news:5.1.0.14.2.20030823170950.05b14e30@localhost...

Zeev Suraski

23 years ago
At 20:40 23/08/2003, l0t3k wrote:
>Zeev, > speaking of const, i have a feature request. why not accept "const" in >the global context and create a compile-time global constant ? >e.g. > > const FOO = 1 ; > const BAR = 2; > > class Barbara { > const Barbara::BUSH = FOO; > }; > >i'd much prefer this as an alternative to the runtime define().
It's going to be runtime either way, because you could do if (...) { const FOO = 1; } else {const FOO = 2 }. Not sure if we want it as syntactic sugar for define() or not. Zeev

Curt Zirzow

23 years ago
* Thus wrote Zeev Suraski (zeev@zend.com):
> At 20:40 23/08/2003, l0t3k wrote: > >Zeev, > > speaking of const, i have a feature request. why not accept "const" in > >the global context and create a compile-time global constant ? > >e.g. > > > > const FOO = 1 ; > > const BAR = 2; > > > > class Barbara { > > const Barbara::BUSH = FOO; > > }; > > > >i'd much prefer this as an alternative to the runtime define(). > > It's going to be runtime either way, because you could do if (...) { const > FOO = 1; } else {const FOO = 2 }. Not sure if we want it as syntactic > sugar for define() or not.
This would be nice! Id like to add perhaps, but not sure how hard it would be, to make this constant (aliased define) unchangeable through out the life time of the script. Curt
-- "I used to think I was indecisive, but now I'm not so sure."

Brad Bulger

23 years ago
hmm. but you can reference class constants without class/self:: when creating futher constants ('const bla = foo << 1;') and this works, too: <?php class foo { const bar = 3; function bla($x) { if ($x === bar) print "yes it's bar\n"; else print "no it's not\n"; } } $x = new foo; $x->bla(4); $x->bla(3); ?>
>> Output: >> no it's not >> yes it's bar
Zeev Suraski wrote:

Zeev Suraski

23 years ago
Yes, there appears to be some inconsistency here. We'll look into it. Zeev At 21:14 23/08/2003, Brad Bulger wrote: