[Solar-talk] proposed usage of constants in place of strings for status in Solar_Auth_Adapter

Raymond Kolbe rkolbe at gmail.com
Mon Mar 24 07:25:26 CDT 2008


+1 Anttih. I was actually talking to a buddy of mine the other day about
const in PHP and how I find I never use it.

On Sun, Mar 23, 2008 at 6:41 PM, Antti Holvikari <anttih at gmail.com> wrote:

> On Mon, Mar 24, 2008 at 12:07 AM, Leo Chiao <leo.chiao at gmail.com> wrote:
> > Thanks for your input Antti, I completely overlooked that aspect and now
> > notice that he is using locale strings to populate the "status_text"
> flash
> > based on the user status string.
> >
> > Do you mind elaborating on your point about clumsy if/else structures?
> If a
> > variable is used soley to signify state or won't change, why would you
> avoid
> > defining a const for it?
>
> Ok, here's a few points:
>
> * You usually use integer values for constants, right, so you'd need
> an if/else to "use" that value somehow. This is one reason why Solar
> doesn't use constants, **string constants** map **directly** to locale
> keys (the "use"). PHP is such a high-level language that often a
> constant value plays a significant role in what will be displayed on
> the page. An integer is the worst value for this. One example: genres.
> You are most likely to display the genre of a user on the page. Using
> a constant for "man" and "women" would mean you'd need to check
> whatever you have in a variable and compare it to the value of a
> constant to be able to echo it.
>
> That said, in a "low-level" PHP library constants could make sense.
> Say, use a different algorithm depending on a constant. This could be
> signaled from a method to another method with a constant:
> readBytes(Class::BIG_ENDIAN). This makes perfect sense. Very clear and
> will never "touch" the UI.
>
> Now, if you used a string for a constant's value I think it would
> kinda defeat the purpose. You'd end up using the string literal itself
> for checking a value than the constant name. I know I would. Wait, I
> could use the literal **without** even the check (which brings to my
> first point).
>
> * Strings are "cheap" in PHP. You will never create a bottleneck with
> using strings instead of constants.
>
> You have a point there that you'd catch some typos with constants.
> IMHO that is alone just not worth using constants.
>
> Every time I've tried using constants I've switched back to plain
> strings. Happy to hear other opinions/experiences.
>
> --
>  Antti Holvikari <http://anttih.com>
> _______________________________________________
> Solar-talk mailing list
> Solar-talk at lists.solarphp.com
> http://mailman-mail3.webfaction.com/listinfo/solar-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman-mail3.webfaction.com/pipermail/solar-talk/attachments/20080324/90bc18fb/attachment.html


More information about the Solar-talk mailing list