Re: Enforce a constant in a class.
| From: | Jochem Maas | Date: | Fri, 22 Jan 2010 17:21:47 +0000 |
| Subject: | Re: Enforce a constant in a class. | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301358@lists.php.net to get a copy of this message | ||
Op 1/22/10 5:19 PM, Richard Quadling schreef:
> 2010/1/22 Jochem Maas <jochem@iamjochem.com>:
>> constants in interfaces are not meant for this. a class constant doesn't
>> constitute an interface. I believe constants in interfaces are allowed purely
>> because it is helpful to have them defined outside of the global space and
>> somewhere where all implementors of said interface can realiably reference them.
>
> Yep.
>
>> I would suggest you need to define some extra methods in your interface e.g.
>>
>> function getKillNotes();
>> function getKillTypeFlag();
>
> The other option would be to be able to _easily_ detect the presence
> of a class constant.
>
> Without an error.
>
> Fatal or otherwise.
>
> $rfClass = ReflecionClass('KilledClass');
> if (in_array('KILL_SWITCH_NOTES', $rfClass->getConstants())) { ...}
>
> seems the only way.
>
> You can't use getConstant('KILL_SWITCH_NOTES') as False is returned
> for failure with no differentiation for a False value. Grrr.
>
defined() ??? besides you can cache the reflection results - and maybe you
only need the reflection stuff when code is in development mode, I'm assuming
your using it to detect coding/developer errors.
>
>
>
> Thanks to you all for the discussion.
>
> Regards,
>
> Richard.
>