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 <nrixham@xxxxxxxxx> 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); > } > } > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php