From: "Gary ." on
Should I be able to do this:

class X
{
const FOO = 'foo';
const FOOBAR = X::FOO . 'bar';

....
}

?

Because I can't. I get "syntax error, unexpected '.', expecting ',' or
';'". I assume this is because the constants are like statics which
can't be initialised by functions etc. but is there really any logic
behind this?
From: Peter Lind on
On 19 April 2010 10:30, Gary . <php-general(a)garydjones.name> wrote:
> Should I be able to do this:
>
> class X
> {
>  const FOO = 'foo';
>  const FOOBAR = X::FOO . 'bar';
>
> ...
> }
>
> ?
>
> Because I can't. I get "syntax error, unexpected '.', expecting ',' or
> ';'". I assume this is because the constants are like statics which
> can't be initialised by functions etc. but is there really any logic
> behind this?
>

It very often pays to read the PHP docs. From
http://pl.php.net/manual/en/language.oop5.constants.php :
"The value must be a constant expression, not (for example) a
variable, a property, a result of a mathematical operation, or a
function call. "

So no, you shouldn't be able to do that.

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
From: "Gary ." on
On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
> On 19 April 2010 10:30, Gary wrote:
>> Should I be able to do this:
>>
>> class X
>> {
>>  const FOO = 'foo';
>>  const FOOBAR = X::FOO . 'bar';
>>
>> ...
>> }

> So no, you shouldn't be able to do that.

Okay. Why not?
From: Peter Lind on
On 19 April 2010 14:24, Gary . <php-general(a)garydjones.name> wrote:
> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
>> On 19 April 2010 10:30, Gary wrote:
>>> Should I be able to do this:
>>>
>>> class X
>>> {
>>>  const FOO = 'foo';
>>>  const FOOBAR = X::FOO . 'bar';
>>>
>>> ...
>>> }
>
>> So no, you shouldn't be able to do that.
>
> Okay. Why not?

Hate to ask, but did you at any point consider to read the PHP docs on
this? The bit I sent or what you could gather from the link posted?

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
From: Ashley Sheridan on
On Mon, 2010-04-19 at 14:37 +0200, Peter Lind wrote:

> On 19 April 2010 14:24, Gary . <php-general(a)garydjones.name> wrote:
> > On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
> >> On 19 April 2010 10:30, Gary wrote:
> >>> Should I be able to do this:
> >>>
> >>> class X
> >>> {
> >>> const FOO = 'foo';
> >>> const FOOBAR = X::FOO . 'bar';
> >>>
> >>> ...
> >>> }
> >
> >> So no, you shouldn't be able to do that.
> >
> > Okay. Why not?
>
> Hate to ask, but did you at any point consider to read the PHP docs on
> this? The bit I sent or what you could gather from the link posted?
>
> --
> <hype>
> WWW: http://plphp.dk / http://plind.dk
> LinkedIn: http://www.linkedin.com/in/plind
> Flickr: http://www.flickr.com/photos/fake51
> BeWelcome: Fake51
> Couchsurfing: Fake51
> </hype>
>


Class constants must be defined with static values, not variables. They
are constants after all! If they relied on the value of a variable,
surely that would mean that their own value might change, so they would
just become regular variables not constants.

Is there a specific reason that you need to try and achieve this? Would
it not be better to have private or protected variables using getters
and setters if you need variable values?

Thanks,
Ash
http://www.ashleysheridan.co.uk