Enforce a constant in a class.

Enforce a constant in a class.

am 22.01.2010 12:33:27 von Richard Quadling

Hello,

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
interface.

Is there a reason for this behaviour?



_OR_

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
const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.

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.

Regards,

Richard.

--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 15:03:46 von Darren Karstens

One way to do it would be to use getter functions in your interface
that return the value of the constant (or a member variable) in your
implemented class. For example:

interface SetKillSwitch {
public function getKillSwitchNotes();
}

Then in your class implement your getKillSwitchNotes function:

class KilledClass implements SetKillSwitch {
const KILL_SWITCH_NOTES =3D 'Some notes';

public function getKillSwitchNotes()
{
return SetKillSwitch::KILL_SWITCH_NOTES;
}
}

On Fri, Jan 22, 2010 at 11:33 AM, Richard Quadling
wrote:
> Hello,
>
> 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
> interface.
>
> Is there a reason for this behaviour?
>
>
>
> _OR_
>
> How do I enforce the presence of a constant in a class?
>
> > interface SetKillSwitch {
> =A0 =A0 =A0 =A0const KILL_SWITCH_SET =3D True;
>
> =A0 =A0 =A0 =A0// Produces an error as no definition exists.
> =A0 =A0 =A0 =A0// const KILL_SWITCH_NOTES;
>
> =A0 =A0 =A0 =A0// Cannot override in any class implementing this interfac=
e.
> =A0 =A0 =A0 =A0const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+0000';
> }
>
> class KilledClass implements SetKillSwitch {
> =A0 =A0 =A0 =A0// Cannot override as defined in interface SetKillSwitch.
> =A0 =A0 =A0 =A0// const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+0000';
> }
> ?>
>
> I want to enforce that any class implementing SetKillSwitch also has a
> const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.
>
> 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.
>
> Regards,
>
> Richard.
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 15:47:55 von Pete Ford

Richard Quadling wrote:
> Hello,
>
> 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
> interface.
>
> Is there a reason for this behaviour?
>
>
>
> _OR_
>
> 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
> const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.
>
> 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.
>
> Regards,
>
> Richard.
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling

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";
}
}

Cheers
Pete Ford

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 16:36:34 von Ashley Sheridan

--=-8pIR3PIUWdDaBv9Q/s4A
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Fri, 2010-01-22 at 11:33 +0000, Richard Quadling wrote:

> Hello,
>=20
> One of the aspects of an interface is to enforce a public view of a
> class (as I see it).
>=20
> Within PHP, interfaces are allowed to have constants, but you cannot
> override them in a class implementing that interface.
>=20
> This seems wrong.
>=20
> 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
> interface.
>=20
> Is there a reason for this behaviour?
>=20
>=20
>=20
> _OR_
>=20
> How do I enforce the presence of a constant in a class?
>=20
> > interface SetKillSwitch {
> const KILL_SWITCH_SET =3D True;
>=20
> // Produces an error as no definition exists.
> // const KILL_SWITCH_NOTES;
>=20
> // Cannot override in any class implementing this interface.=09
> const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+0000';
> }
>=20
> class KilledClass implements SetKillSwitch {
> // Cannot override as defined in interface SetKillSwitch.
> // const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+0000';
> }
> ?>
>=20
> I want to enforce that any class implementing SetKillSwitch also has a
> const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.
>=20
> I have to use reflection to see if the constant exists and throw an
> exception when it doesn't.
>=20
> The interface should only say that x, y and z must exist, not the
> values of x, y and z.
>=20
> Regards,
>=20
> Richard.
>=20
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D2134=
74731
> ZOPA : http://uk.zopa.com/member/RQuadling
>=20


Constants are there for things that should never change. If you ever
need to change them, then whoever created the base class either didn't
think things through properly, or you're not. Imagine a class that sets
the value of π (as is the erstwhile example for constants) If further
classes that implemented it were of a particular historical persuasion,
then they might want to redefine the constant as just 3. It seemed like
a good idea to the historical Roman Catholics at the time (it was
defined as 3 in the Bible after all) but it doesn't make it the correct
value (imagine the problems with volume calculations!)

Constants are so called for a good reason!

And for those interested, my source for the value of π is
http://gospelofreason.wordpress.com/2007/06/13/god-said-pi-3 -stand-by-your-=
beliefs-dammit/


Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-8pIR3PIUWdDaBv9Q/s4A--

Re: Re: Enforce a constant in a class.

am 22.01.2010 16:51:56 von Richard Quadling

2010/1/22 Pete Ford :
>
> 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 =3D TRUE;
>        protected static $notes;
>        protected static $date =3D '2010-01-22T11:23:3=
2+0000';
> }
>
> class KilledClass implements SetKillSwitch
> {
>        public function __construct()
>        {
>                self::$isSet =3D F=
ALSE;
>                self::$date =3D '2=
010-01-21T09:30:00+0000';
>                self::$notes =3D "=
Test";
>        }
> }
>
> Cheers
> Pete Ford

There is the problem. The interface is just saying that a killswitch
has been set. Not the date it was set, nor the notes. Thats specific
to the implentor.

In the implementor, they are constant.

In my mind, the interface is only saying that these elements need to
exist. Not what they are. Just like if I say there must be a method
X() in the interface, I don't define what it does.


--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 16:55:57 von Richard Quadling

2010/1/22 Ashley Sheridan
> Constants are there for things that should never change. If you ever need=
to change them, then whoever created the base class either didn't think th=
ings through properly, or you're not. Imagine  a class that sets the v=
alue of π (as is the erstwhile example for constants) If further class=
es that implemented it were of a particular historical persuasion, then the=
y might want to redefine the constant as just 3. It seemed like a good idea=
to the historical Roman Catholics at the time (it was defined as 3 in the =
Bible after all) but it doesn't make it the correct value (imagine the prob=
lems with volume calculations!)
>
> Constants are so called for a good reason!

And in the class that I want to enforce the presence of the constant,
it will be constant.

I just want to enforce the presence.


--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 17:00:54 von Richard Quadling

2010/1/22 Darren Karstens :
> One way to do it would be to use getter functions in your interface
> that return the value of the constant (or a member variable) in your
> implemented class. For example:
>
> interface SetKillSwitch {
>       public function getKillSwitchNotes();
> }
>
> Then in your class implement your getKillSwitchNotes function:
>
> class KilledClass implements SetKillSwitch {
>       const KILL_SWITCH_NOTES =3D 'Some notes';
>
>      public function getKillSwitchNotes()
>      {
>          return SetKillSwitch::KILL_SWITCH_NOTES=
;
>      }
> }
>

The killswitch is something I want to tag to a class (KILL_SWITCH_SET)

The class itself will have to be documented as to why it has been
killed off (KILLED_ON and KILL_SWITCH_NOTES).




--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Enforce a constant in a class.

am 22.01.2010 17:02:09 von Richard Quadling

2010/1/22 Pete Ford :
> 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 =3D TRUE;
>        protected static $notes;
>        protected static $date =3D '2010-01-22T11:23:3=
2+0000';
> }
>
> class KilledClass implements SetKillSwitch
> {
>        public function __construct()
>        {
>                self::$isSet =3D F=
ALSE;
>                self::$date =3D '2=
010-01-21T09:30:00+0000';
>                self::$notes =3D "=
Test";
>        }
> }
>
> Cheers
> Pete Ford

And of course, "Fatal error: Interfaces may not include member variables".



--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 17:12:36 von Jochem Maas

Op 1/22/10 4:55 PM, Richard Quadling schreef:
> 2010/1/22 Ashley Sheridan
>> Constants are there for things that should never change. If you ever need to change them, then whoever created the base class either didn't think things through properly, or you're not. Imagine a class that sets the value of π (as is the erstwhile example for constants) If further classes that implemented it were of a particular historical persuasion, then they might want to redefine the constant as just 3. It seemed like a good idea to the historical Roman Catholics at the time (it was defined as 3 in the Bible after all) but it doesn't make it the correct value (imagine the problems with volume calculations!)
>>
>> Constants are so called for a good reason!
>
> And in the class that I want to enforce the presence of the constant,
> it will be constant.
>
> I just want to enforce the presence.

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.

I would suggest you need to define some extra methods in your interface e.g.

function getKillNotes();
function getKillTypeFlag();

>
>
> --
> -----
> Richard Quadling
> "Standing on the shoulders of some very clever giants!"
> EE : http://www.experts-exchange.com/M_248814.html
> EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
> Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
> ZOPA : http://uk.zopa.com/member/RQuadling
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 17:19:15 von Richard Quadling

2010/1/22 Jochem Maas :
> constants in interfaces are not meant for this. a class constant doesn't
> constitute an interface. I believe constants in interfaces are allowed pu=
rely
> because it is helpful to have them defined outside of the global space an=
d
> somewhere where all implementors of said interface can realiably referenc=
e 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 =3D 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.




Thanks to you all for the discussion.

Regards,

Richard.

--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 18:21:47 von Jochem Maas

Op 1/22/10 5:19 PM, Richard Quadling schreef:
> 2010/1/22 Jochem Maas :
>> 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.
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 22.01.2010 18:24:52 von Richard Quadling

2010/1/22 Jochem Maas :
> defined()

Sits.

Ponders.

Find's gun.

Shoots self!




--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Enforce a constant in a class.

am 25.01.2010 11:36:52 von Pete Ford

Richard Quadling wrote:
> 2010/1/22 Pete Ford :
>> 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";
>> }
>> }
>>
>> Cheers
>> Pete Ford
>
> And of course, "Fatal error: Interfaces may not include member variables".
>
>
>

Ooops, sorry :)

I tend to end up using abstract base classes rather than interfaces for
that sort of reason...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Enforce a constant in a class.

am 25.01.2010 12:35:40 von Richard Quadling

2010/1/25 Pete Ford :
> Richard Quadling wrote:
>>
>> 2010/1/22 Pete Ford :
>>>
>>> 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, a=
nd
>>> use the implementation's constructor to set the implementation-specific
>>> value (or override the default)
>>>
>>> interface SetKillSwitch
>>> {
>>>       protected static $isSet =3D TRUE;
>>>       protected static $notes;
>>>       protected static $date =3D '2010-01-22T11:23:32+00=
00';
>>> }
>>>
>>> class KilledClass implements SetKillSwitch
>>> {
>>>       public function __construct()
>>>       {
>>>               self::$isSet =3D FALSE=
;
>>>               self::$date =3D '2010-=
01-21T09:30:00+0000';
>>>               self::$notes =3D "Test=
";
>>>       }
>>> }
>>>
>>> Cheers
>>> Pete Ford
>>
>> And of course, "Fatal error: Interfaces may not include member variables=
".
>>
>>
>>
>
> Ooops, sorry :)
>
> I tend to end up using abstract base classes rather than interfaces for t=
hat
> sort of reason...
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Essentially I was starting with the idea that a subclass with constant
X _MUST_ have constant Y and Z. That's what I wanted to enforce.

But, finding that defined() was enough, I now realize that Y and Z are
_not_ mandatory, but they are constants. So simply ...

$KillSwitchNotes =3D defined(get_called_class() . '::KILL_SWITCH_NOTES') ?:=
Null;

is enough and now the whole _MUST_ is gone and is now optional.

Much better.


Thank you to everyone who chipped in. Old dog should really have known
that old trick!


--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Enforce a constant in a class.

am 25.01.2010 23:53:22 von Colin Guthrie

'Twas brillig, and Richard Quadling at 22/01/10 11:33 did gyre and gimble:
> Hello,
>
> 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
> interface.
>
> Is there a reason for this behaviour?
>
>
>
> _OR_
>
> 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
> const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.
>
> 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.

Forgive the perhaps silly question but why are you requiring to use
constants here.

I appreciate the desire to use Reflection but why not just define a
method that must be implemented in the interface?

interface SetKillSwitch {
public function getKillDate();
public function getKillNotes();
}


By virtue of something impementing the interface, you know the methods
will exist.

If you want to make implmentation of classes easier, then define and
abstract class with an appropriate constructor and implementation:


abstract class SetKillSwitchAbstract {
private $_killDate;
private $_killNotes;
protected function __construct($killDate, $killNotes)
{
$this->_killDate = $killDate;
$this->_killNotes = $killNotes;
}

public function getKillDate()
{
return $this->_killDate;
}

public function getKillNotes()
{
return $this->_killNotes;
}
}


You can either put your "implements SetKillSwitch" in this class or the
derived classes depending on other methods you want to provide in the
base class.


I don't see why constants specifically are needed here. Rather than
using reflection you can just use instanceof or similar to tell if a
given object implements the interface or simply use the interface name
as a type specifier on an argument to another function/method etc.


Col


--

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
Mandriva Linux Contributor [http://www.mandriva.com/]
PulseAudio Hacker [http://www.pulseaudio.org/]
Trac Hacker [http://trac.edgewall.org/]


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Re: Enforce a constant in a class.

am 26.01.2010 11:58:55 von Richard Quadling

2010/1/25 Colin Guthrie :
> 'Twas brillig, and Richard Quadling at 22/01/10 11:33 did gyre and gimble=
:
>> Hello,
>>
>> 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
>> interface.
>>
>> Is there a reason for this behaviour?
>>
>>
>>
>> _OR_
>>
>> How do I enforce the presence of a constant in a class?
>>
>> >> interface SetKillSwitch {
>>       const KILL_SWITCH_SET =3D True;
>>
>>       // Produces an error as no definition exists.
>>       // const KILL_SWITCH_NOTES;
>>
>>       // Cannot override in any class implementing this i=
nterface.
>>       const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+000=
0';
>> }
>>
>> class KilledClass implements SetKillSwitch {
>>       // Cannot override as defined in interface SetKillS=
witch.
>>       // const KILL_SWITCH_DATE =3D '2010-01-22T11:23:32+=
0000';
>> }
>> ?>
>>
>> I want to enforce that any class implementing SetKillSwitch also has a
>> const KILL_SWITCH_DATE and a const KILL_SWITCH_NOTES.
>>
>> 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.
>
> Forgive the perhaps silly question but why are you requiring to use
> constants here.
>
> I appreciate the desire to use Reflection but why not just define a
> method that must be implemented in the interface?
>
> interface SetKillSwitch {
>  public function getKillDate();
>  public function getKillNotes();
> }
>
>
> By virtue of something impementing the interface, you know the methods
> will exist.
>
> If you want to make implmentation of classes easier, then define and
> abstract class with an appropriate constructor and implementation:
>
>
> abstract class SetKillSwitchAbstract {
>  private $_killDate;
>  private $_killNotes;
>  protected function __construct($killDate, $killNotes)
>  {
>    $this->_killDate =3D $killDate;
>    $this->_killNotes =3D $killNotes;
>  }
>
>  public function getKillDate()
>  {
>    return $this->_killDate;
>  }
>
>  public function getKillNotes()
>  {
>    return $this->_killNotes;
>  }
> }
>
>
> You can either put your "implements SetKillSwitch" in this class or the
> derived classes depending on other methods you want to provide in the
> base class.
>
>
> I don't see why constants specifically are needed here. Rather than
> using reflection you can just use instanceof or similar to tell if a
> given object implements the interface or simply use the interface name
> as a type specifier on an argument to another function/method etc.
>
>
> Col
>
>
> --
>
> Colin Guthrie
> gmane(at)colin.guthr.ie
> http://colin.guthr.ie/
>
> Day Job:
>  Tribalogic Limited [http://www.tribalogic.net/]
> Open Source:
>  Mandriva Linux Contributor [http://www.mandriva.com/]
>  PulseAudio Hacker [http://www.pulseaudio.org/]
>  Trac Hacker [http://trac.edgewall.org/]
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

With a constant, PhpDoc will pick up the value of the constant and
incorporate it into the documentation.

With a method, there is no way to know the return value. The type,
sure, but not the value.

Setting a constant is by far the simplest way to deal with this.

A method to return a constant is one method unneeded.

But, as I've said, I completely missed defined().

That's all I needed.

If the kill interface is applied to a class, then the parent class can
quite happily use defined(get_called_class() . '::KILL_SWITCH_SET') to
see if the class is defunct (or whatever).

Really. It was just the defined() call I missed. Everything is now working =
fine.

--=20
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=3DZEND002498&r=3D213474=
731
ZOPA : http://uk.zopa.com/member/RQuadling

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php