Search code examples
phpajaxcodeignitermagic-methods

Help using __call with codeigniter


I have a class in codeigniter which deals solely with ajax. I have created a function within this class which checks if the refferal is an ajax refferal, I want to be able to call this function every time that any function from this class is used.

As such I've implemented the __call magic method

class Ajax_content extends Controller {

    function __construct()
    {
        parent::Controller();


    }

    function __call($method, $arguments){

            $this->ajaxCheck(); //set up to return false and exit.
            call_user_func_array(array($this,"_".$method),$arguments);

        }

At present ajaxCheck() always returns false and exit()s. But it's not being called (at present my ajax request still returns data) Is this a valid way of approaching the problem?


Solution

  • I want to be able to call this function every time that any function from this class is used

    __call is only triggered when invoking inaccessible methods. If you want certain functionality to be executed every time a controller method is called (and by that, I assume you mean a method that's mapped to a web request, not any function in the class), you should put this functionality in the constructor.