Richard Quadling wrote:

One of the aspects of an interface is to enforce a public view of a
class (as I see it).

Within PHP, interfaces are allowed to have constants, but you cannot
override them in a class implementing that interface.

This seems wrong.

The interface shouldn't define the value, just like it doesn't define
the content of the method, it only defines its existence and requires
that a class implementing the interface accurately matches the

Is there a reason for this behaviour?


How do I enforce the presence of a constant in a class?

interface SetKillSwitch {
        const KILL_SWITCH_SET = True;

        // Produces an error as no definition exists.
        // const KILL_SWITCH_NOTES;

        // Cannot override in any class implementing this interface.    
        const KILL_SWITCH_DATE = '2010-01-22T11:23:32+0000';

class KilledClass implements SetKillSwitch {
        // Cannot override as defined in interface SetKillSwitch.
        // const KILL_SWITCH_DATE = '2010-01-22T11:23:32+0000';

I want to enforce that any class implementing SetKillSwitch also has a

I have to use reflection to see if the constant exists and throw an
exception when it doesn't.

The interface should only say that x, y and z must exist, not the
values of x, y and z.



Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE :
EE4Free :
Zend Certified Engineer :

IMHO, a constant is not the correct beastie in this case - if you want it to be different depending on the implementation then it ain't a constant!

You should probably have protected static variables in the interface, and use the implementation's constructor to set the implementation-specific value (or override the default)

interface SetKillSwitch
        protected static $isSet = TRUE;
        protected static $notes;
        protected static $date = '2010-01-22T11:23:32+0000';

class KilledClass implements SetKillSwitch
        public function __construct()
                self::$isSet = FALSE;
                self::$date = '2010-01-21T09:30:00+0000';
                self::$notes = "Test";

Pete Ford

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to