Re: [PHP] Enforce a constant in a class.
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: [PHP] Enforce a constant in a class.
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: [PHP] Enforce a constant in a class.
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. -- - 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] Enforce a constant in a class.
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. Thanks to you all for the discussion. 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: [PHP] Enforce a constant in a class.
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: [PHP] Enforce a constant in a class.
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 = '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). -- - 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] Enforce a constant in a class.
On Fri, 2010-01-22 at 11:33 +, 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+'; > } > > 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. > > 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 > 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
Re: [PHP] Enforce a constant in a class.
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 = '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 { > 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. > > 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 > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Enforce a constant in a class.
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php