1

I want to check that user has permissions to invoke some method.

This example works fine

class A
{   
    public function __call($method, $args)
    {
        echo 'do something before invoke method</br>';

        if(method_exists($this, $method)) 
        {
            call_user_func_array(array($this, $method), $args);
        }
    }


    private function testA()
    {
        echo 'do something what testA has to do';
    }
}

but I want to do the validation in every class for every method I thought that this solution will be good for me

class A
{
    public function __call($method, $args)
    {
        echo 'do something before invoke method</br>';

        if(method_exists($this, $method)) 
        {
            call_user_func_array(array($this, $method), $args);
        }
    }

}

class B extends A
{
    private function testB()
    {
        echo 'do something what testB has to do</br>';
    }
}

I was wrong. I get infinite loop. I need one basic class that will validate user's permission before invoking some method.

Antony
  • 14,900
  • 10
  • 46
  • 74
  • 3
    Instead of inheritance, you may consider the [decorator pattern](http://en.wikipedia.org/wiki/Decorator_pattern). Take a look at [this](http://stackoverflow.com/questions/3430181/acl-implementation) as well. – Antony Dec 30 '13 at 11:31
  • Thank you. This solution works. I was thinking more about something what I use / implement once and later I don't have to remember that I have checked permissions or not. On the other hand it will validate every method. In your solution I can choose when and which method I validate. – user3146156 Dec 30 '13 at 14:01

0 Answers0