Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__call equivalent for public methods

I have an API for interacting with my web app, defined by a class. Each publicly accessible method needs to have authentication done before running. Rather than putting the same line over and over in each method, I'd like to use the magic __call function. However, it will only work on private or protected methods, and mine need to be public in order to work with Zend_Json_Server.

class MY_Api
{
  public function __call($name, $arguments)
  {
    //code here that checks arguments for valid auth token and returns an error if false
  }

  public function myFunction($param1, $param2, $param3)
  {
    //do stuff when the user calls the myFunction and passes the parameters
    //this function must remain public so that Zend_Json_Server can parse it
    //but I want it intercepted by a magic method so that the authentication
    //can be checked and the system bails before it even gets to this function.
  }
}

Is it possible to hook into these public functions and possibly cancel their execution before they are called?

like image 652
Bob Baddeley Avatar asked Jul 13 '11 20:07

Bob Baddeley


People also ask

What is __ call () in PHP?

When you call a method on an object of the Str class and that method doesn't exist e.g., length() , PHP will invoke the __call() method. The __call() method will raise a BadMethodCallException if the method is not supported. Otherwise, it'll add the string to the argument list before calling the corresponding function.

What is__ invoke?

The __invoke() method is called when a script tries to call an object as a function. Example #4 Using __invoke() class CallableClass. { public function __invoke($x)

What are call methods?

Method Calls A method is a routine that applies to a particular class of objects. Once an object is declared, you can refer to it by its identifier when calling methods. You can call methods for a window that has not previously been declared using a special identifier for dynamic instantiation.

Can a public method be called by any other method?

A public method can be called by any other method. A class method can be called from the class itself. Methods that are not class methods must be called from an instantiated object of that class. The return type void means that the method will return a value.


1 Answers

__call actually works for all methods, including public. However, the reason it won't work if the public method already exists, is because code outside your class can already access the public members. __call is only invoked for members which aren't accessible by the calling code.

As far as I know there are really no options to do what you're looking for, except by using some kind of a decorator pattern:

class AuthDecorator {
    private $object;

    public function __construct($object) {
        $this->object = $object;
    }

    public function __call($method, $params) {
        //Put code for access checking here

        if($accessOk) {
            return call_user_func_array(array($this->object, $method), $params);
        }
    }
}

$api = new MY_Api();
$decoratedApi = new AuthDecorator($api);

//any calls to decoratedApi would get an auth check, and if ok, 
//go to the normal api class' function
like image 155
Jani Hartikainen Avatar answered Oct 12 '22 05:10

Jani Hartikainen