NYCPHP Meetup

NYPHP.org

[nycphp-talk] Constants interfaces

Michael B Allen ioplex at gmail.com
Wed Sep 3 02:07:29 EDT 2008


On Wed, Sep 3, 2008 at 12:34 AM, Anirudh Zala <arzala at gmail.com> wrote:
> On Wednesday 03 September 2008 06:04:16 Michael B Allen wrote:
>> How do people feel about the use of Java-ist "Constants" interfaces like:
>>
>>   interface Sql_Constants
>>   {
>>
>>       const SELECT = 1;
>>       const FROM = 2;
>>       const WHERE = 3;
>>       const NOT = 4;
>>       ....
>>
>>   }
>>
>> where the objective is to permit any class that needs these constants
>> to access them using "self" like:
>>
>>   require_once 'Sql/Constants.php';
>>
>>   class Sql_Parser implements Sql_Constants
>>   {
>>
>>       public function parse($sql)
>>       {
>>           ...
>>           if ($tokenCode === self::WHERE) {
>
> Hello Mike
>
> Defining interface constants, according to me, is not good design pattern even
> if it is enabled later in PHP.
>
> Reason behind this is that Interfaces are primarily designed to define types
> and not to hold data.

That would not be true if the constants represented an enum or more
generally values that never change.

An enum defines a type that can be a specific subset of values. I
think that qualifies as part of an interface definition.

A constant could be thought of as a method that accepts no parameters
and always returns the same value.

Consider the following:

  interface Math_Constants {

      const PI = 3.14159265;

I think PI would qualify as part of an interface definition. The value
of PI will never change.

However, if for example the interface were used to "define" database
connection parameters then that would be a gross violation of an
interface definition (and a poor use of const in general).

Mike

-- 
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/



More information about the talk mailing list