Re: [PHP] Re: Enforce a constant in a class.
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 = 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+'; >> } >> >> class KilledClass implements SetKillSwitch { >> // Cannot override as defined in interface SetKillSwitch. >> // const KILL_SWITCH_DATE = '2010-01-22T11:23:32+'; >> } >> ?> >> >> 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 > > 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. -- - 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
[PHP] Re: Enforce a constant in a class.
'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+'; > } > > class KilledClass implements SetKillSwitch { > // Cannot override as defined in interface SetKillSwitch. > // const KILL_SWITCH_DATE = '2010-01-22T11:23:32+'; > } > ?> > > 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: [PHP] Re: Enforce a constant in a class.
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, 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+'; >>> } >>> >>> class KilledClass implements SetKillSwitch >>> { >>> public function __construct() >>> { >>> self::$isSet = FALSE; >>> self::$date = '2010-01-21T09:30:00+'; >>> 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 > > 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 = 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! -- - 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: [PHP] Re: Enforce a constant in a class.
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+'; } class KilledClass implements SetKillSwitch { public function __construct() { self::$isSet = FALSE; self::$date = '2010-01-21T09:30:00+'; 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: [PHP] Re: Enforce a constant in a class.
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+'; > } > > class KilledClass implements SetKillSwitch > { > public function __construct() > { > self::$isSet = FALSE; > self::$date = '2010-01-21T09:30:00+'; > self::$notes = "Test"; > } > } > > Cheers > Pete Ford And of course, "Fatal error: Interfaces may not include member variables". -- - 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: [PHP] Re: Enforce a constant in a class.
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+'; > } > > class KilledClass implements SetKillSwitch > { > public function __construct() > { > self::$isSet = FALSE; > self::$date = '2010-01-21T09:30:00+'; > self::$notes = "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. -- - 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
[PHP] Re: Enforce a constant in a class.
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? 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+'; } class KilledClass implements SetKillSwitch { public function __construct() { self::$isSet = FALSE; self::$date = '2010-01-21T09:30:00+'; self::$notes = "Test"; } } Cheers Pete Ford -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php