Class constants
am 19.04.2010 10:30:26 von php-general
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?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 10:36:02 von Peter Lind
On 19 April 2010 10:30, Gary . wrote:
> Should I be able to do this:
>
> class X
> {
> Â const FOO =3D 'foo';
> Â const FOOBAR =3D 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.
--=20
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 14:24:09 von php-general
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
>> {
>> =A0const FOO =3D 'foo';
>> =A0const FOOBAR =3D X::FOO . 'bar';
>>
>> ...
>> }
> So no, you shouldn't be able to do that.
Okay. Why not?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 14:37:02 von Peter Lind
On 19 April 2010 14:24, Gary . 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 =3D 'foo';
>>> Â const FOOBAR =3D 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?
--=20
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 15:12:22 von Ashley Sheridan
--=-Dp6FmdXo+2YhW+Qt3abW
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Mon, 2010-04-19 at 14:37 +0200, Peter Lind wrote:
> On 19 April 2010 14:24, Gary . 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?
>
> --
>
> 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
>
>
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
--=-Dp6FmdXo+2YhW+Qt3abW--
Re: Class constants
am 19.04.2010 16:18:19 von php-general
On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind wrote:
> On 19 April 2010 14:24, Gary wrote:
>> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
>>> 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?
Yes. The question remains.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 16:25:20 von Peter Lind
On 19 April 2010 16:18, Gary . wrote:
> On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind wrote:
>> On 19 April 2010 14:24, Gary wrote:
>>> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
>
>>>> 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?
>
> Yes. The question remains.
>
Per the PHP manual: "The value must be a constant expression". Is
something that depends on other classes, variables or functions
constant?
If you're asking why a constant should be constant, I can only point
you to Ashleys answer or google.
Regards
Peter
--
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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 16:29:18 von php-general
On Mon, Apr 19, 2010 at 3:12 PM, Ashley Sheridan wrote:
> On 19 April 2010 14:24, Gary wrote:
> > Okay. Why not?
....
> Class constants must be defined with static values, not variables. They a=
re constants after all! If they relied on the value of a variable, surely t=
hat would mean that their own value might change, so they would just become=
regular variables not constants.
Right. But in fact the only referenced values are *also* constant
(unless I'm completely misunderstanding the use of 'const'), so I
think it's a valid thing to want to do. I accept it doesn't seem to be
possible, I'm now curious as to the thinking behind it.
> Is there a specific reason that you need to try and achieve this?
Okay, well here's a more meaningful situation, perhaps:
class SomeTable
{
const TABLE_NAME =3D 'mytable';
const SELECT =3D 'SELECT * FROM ' . MyTable::TABLE_NAME;
const INSERT =3D 'INSERT INTO ' . MyTable::TABLE_NAME ...
If the table name should change for some reason, it is preferable to
make the change in one place in the code (i.e. the value of
TABLE_NAME), surely, than changing the name in the alternative, which
is something like:
class SomeTable
{
// const TABLE_NAME =3D 'mytable';
const SELECT =3D 'SELECT * FROM mytable';
const INSERT =3D 'INSERT INTO mytable...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Class constants
am 19.04.2010 18:22:57 von Adam Richardson
--00163600d67bb1f0820484995ff5
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Apr 19, 2010 at 10:25 AM, Peter Lind wrote:
> On 19 April 2010 16:18, Gary . wrote:
> > On Mon, Apr 19, 2010 at 2:37 PM, Peter Lind
> wrote:
> >> On 19 April 2010 14:24, Gary wrote:
> >>> On Mon, Apr 19, 2010 at 10:36 AM, Peter Lind wrote:
> >
> >>>> 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?
> >
> > Yes. The question remains.
> >
>
> Per the PHP manual: "The value must be a constant expression". Is
> something that depends on other classes, variables or functions
> constant?
> If you're asking why a constant should be constant, I can only point
> you to Ashleys answer or google.
>
> Regards
> Peter
>
> --
>
> 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
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Hi Gary,
Sorry for the confusion. I understand your question. For the life of those
variables, they would never change If you come from other programming
language backgrounds, this can be a bit confusing. In Objective C, the term
"constant expression" is one in which every element is a constant, so given
the requirement that "The value must be a constant expression", your
examples would make sense. Additionally, in languages like Scala, values
default to a constant, and you can compose them in a myriad of ways.
I don't claim to know the rationale, but once you get used to it, it's not a
big deal. I just tend to use classes that allow a value to be set once and
only once for these types of situations.
Happy coding ;)
Adam
--
Nephtali: PHP web framework that functions beautifully
http://nephtaliproject.com
--00163600d67bb1f0820484995ff5--
Re: Class constants
am 19.04.2010 22:34:34 von David Harkness
--000e0cd719aa991fa804849ce3b0
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Apr 19, 2010 at 7:25 AM, Peter Lind wrote:
> Per the PHP manual: "The value must be a constant expression". Is
> something that depends on other classes, variables or functions
> constant?
>
When I came up against this problem myself, I read "a constant expression"
to mean "an expression involving only constants". I think that's a fairly
reasonable parse of the phrase. Thus I expected to be able to combine other
constants using the dot operator as Gary is trying to do. Obviously, it
didn't work, and I removed the "base" constant and replicated its value in
the other constants--a situation constants were designed to obviate.
Gary, you'd probably be better off asking for the technical reason behind
this limitation on a -devel list. :)
--
David Harkness
Senior Software Engineer
High Gear Media, Inc.
--000e0cd719aa991fa804849ce3b0--
Re: Class constants
am 20.04.2010 19:37:11 von Nathan Rixham
Gary . wrote:
> On Mon, Apr 19, 2010 at 3:12 PM, Ashley Sheridan wrote:
>> On 19 April 2010 14:24, Gary wrote:
>
>>> Okay. Why not?
> ...
>> 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.
a constant is something constant (doesn't change), something static is
something static (persistently there, but may change)
> Right. But in fact the only referenced values are *also* constant
> (unless I'm completely misunderstanding the use of 'const'), so I
> think it's a valid thing to want to do. I accept it doesn't seem to be
> possible, I'm now curious as to the thinking behind it.
>
>> Is there a specific reason that you need to try and achieve this?
>
> Okay, well here's a more meaningful situation, perhaps:
> class SomeTable
> {
> const TABLE_NAME = 'mytable';
> const SELECT = 'SELECT * FROM ' . MyTable::TABLE_NAME;
> const INSERT = 'INSERT INTO ' . MyTable::TABLE_NAME ...
>
try
public static $INSERT = 'INSERT INTO ' . MyTable::$TABLE_NAME ...
self::$INSERT
> If the table name should change for some reason, it is preferable to
> make the change in one place in the code (i.e. the value of
> TABLE_NAME), surely, than changing the name in the alternative, which
> is something like:
> class SomeTable
> {
> // const TABLE_NAME = 'mytable';
> const SELECT = 'SELECT * FROM mytable';
> const INSERT = 'INSERT INTO mytable...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php