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

Antti Holvikari anttih at gmail.com
Sun Mar 23 16:49:36 CDT 2008


On Sun, Mar 23, 2008 at 11:41 PM, Leo Chiao <leo.chiao at gmail.com> wrote:
> Paul,
>
> Is there any reason why you chose to use strings such as INVALID, IDLED, etc
> as the status indicators in Solar_Auth_Adapter? I'm thinking that a class
> level constant would be more suited for this purpose.  In the scenario where
> someone might want to check the status, using constants would allow for
> easier debugging if someone mistyped the status string code.  .
>
> Let's assume you add constants named STATUS_IDLED, STATUS_VALID, etc. and
> $user is a Solar_User instance:
>
> if ($user->auth->status == 'IDLE') {
>
> }
>
> versus
>
> if ($user->auth->status == Solar_Auth_Adapter::STATUS_IDLE) {
>
> }
>
>
> In the first case, the if statement would never get run due to the typo of
> "IDLE" vs "IDLED" and no error would be shown. Using a constant, you would
> see an error and be able to recognize the source of the typo much easily.

Simple, strings work with localization strings. Personally, my opinion
is that constants shouldn't be used *at all* with PHP. They lead to
very clumsy if/else structures which I think something as dynamic as
PHP code can't afford.

Sorry, I'm not Paul :-(. Just had to give my $0.02.

-- 
Antti Holvikari <http://anttih.com>


More information about the Solar-talk mailing list