Re: Re: how to overload accessible methods

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux