Re: how to overload accessible methods

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

 



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


[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