Re: [PHP] Re: how to overload accessible methods
thanks for all your reply, since all these classes have main entry in cron.php, I have moved validation there. Richard, your solution seems interesting, maybe I can use it later :) On Thu, Apr 15, 2010 at 4:46 AM, Richard Quadling wrote: > On 13 April 2010 17:25, Ryan Sun wrote: >> this is a class for corntab job, and the validation is very simple, >> just check if the status of user is active when cron job runs, if not, >> throws an exception, other developers won't want to overwrite this >> validation. >> which method of user class will be called is configurable via website >> backed page(we write the name of methods directly in to schedule >> table). >> Using private methods will solve the problem but since we write public >> methods for all the other cron classes, I just want to keep the style >> to make less confusion. >> >> On Tue, Apr 13, 2010 at 12:11 PM, Nathan Rixham wrote: >>> Ryan Sun wrote: I'm writing an abstract parent class which only contain a validate method, other developers will extend this class and add many new public methods, every new methods will need to perform a validate first. Won't it be good if validate get called automatically before every method call so that they don't have to write more code and they won't miss this validate? >>> >>> This may call for a back to roots approach, what exactly are you trying >>> to accomplish, as in: what is the validation doing? >>> >>> perhaps if we see the full picture, we can recommend another perhaps >>> more suited approach to the full thing, feel free to post the full code >>> if you want / can, the more info the better! >>> >>> Regards, >>> >>> Nathan >>> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > > Would this be better ... > > abstract class baseValidate() { > final public function __construct($params) { > // Determine if user is active. > // If OK, then call abstract function postConstruct($params) > } > > abstract function postConstruct($params); > } > > > You can't override the constructor, so the validation will always be > called. The developer's can implement their own postConstruct as if > they where extending __construct. > > > > -- > - > 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: how to overload accessible methods
On 13 April 2010 17:25, Ryan Sun wrote: > this is a class for corntab job, and the validation is very simple, > just check if the status of user is active when cron job runs, if not, > throws an exception, other developers won't want to overwrite this > validation. > which method of user class will be called is configurable via website > backed page(we write the name of methods directly in to schedule > table). > Using private methods will solve the problem but since we write public > methods for all the other cron classes, I just want to keep the style > to make less confusion. > > On Tue, Apr 13, 2010 at 12:11 PM, Nathan Rixham wrote: >> Ryan Sun wrote: >>> I'm writing an abstract parent class which only contain a validate >>> method, other developers will extend this class and add many new >>> public methods, every new methods will need to perform a validate >>> first. Won't it be good if validate get called automatically before >>> every method call so that they don't have to write more code and they >>> won't miss this validate? >> >> This may call for a back to roots approach, what exactly are you trying >> to accomplish, as in: what is the validation doing? >> >> perhaps if we see the full picture, we can recommend another perhaps >> more suited approach to the full thing, feel free to post the full code >> if you want / can, the more info the better! >> >> Regards, >> >> Nathan >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Would this be better ... abstract class baseValidate() { final public function __construct($params) { // Determine if user is active. // If OK, then call abstract function postConstruct($params) } abstract function postConstruct($params); } You can't override the constructor, so the validation will always be called. The developer's can implement their own postConstruct as if they where extending __construct. -- - 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: how to overload accessible methods
On Tue, 2010-04-13 at 12:03 -0400, Ryan Sun wrote: > I'm writing an abstract parent class which only contain a validate > method, other developers will extend this class and add many new > public methods, every new methods will need to perform a validate > first. Won't it be good if validate get called automatically before > every method call so that they don't have to write more code and they > won't miss this validate? > > On Tue, Apr 13, 2010 at 11:46 AM, Nathan Rixham wrote: > > Ryan Sun wrote: > >> As we all know, __call() can overload non-accessible methods, > >> eg. > >> Class User > >> { > >> public function __call($name, $args) > >> { > >> //validate user > >> $this->_validate(); > >> > >> $this->_{$name}($args); > >> } > >> private function _validate() > >> { > >> // > >> } > >> private function _update($args) > >> { > >> // > >> } > >> } > >> > >> $user = new User(); > >> $user->update() // will call _validate before _update automatically > >> > >> BUT, if I want to make this update a public function, how can I call > >> the validate without call it inside update function explicitly? > > > > > > why would you want to, is there a technical reason for wanting magic > > functionality instead of "normal" functionality (+ wouldn't it make the > > code much easier to maintain and debug if developers can see what is > > called where, instead of just magic). > > > > to answer though, you're best bet is probably to make an abstract class > > with magic functionality and then extend with an implementing public class. > > > > abstract class MagicUser > > { > >public function __call($name, $args) > >{ > >//validate user > >$this->_validate(); > >$this->_{$name}($args); > >} > > > >private function _validate() > >{ > >// > >} > >private function _update($args) > >{ > >// > >} > > } > > > > class User extends MagicUser > > { > >public function update($args) > >{ > >parent::update($args); > >} > > } > > > > > That would mean that the class can only be extended as far as your validation code allows, and the developers extending your class will have little control over how anything is validated. Thanks, Ash http://www.ashleysheridan.co.uk