0

I'm getting the old familiar "Fatal error: Using $this when not in object context" referring to $this->test = 'test'; in the following class:

class Example {
    public $test;
    public function index() {
        $this->test = 'test';
    }
}

The class method is called via call_user_func_array(array('example', 'index'), $params);. I can only assume that call_user_func_array has for some reason decided to call the index method as static e.g. example::index()? However I haven't figured out a fix for this and oddly I've not had a problem with it until recently.

1 Answer 1

5

This works:

$obj = new Example();
call_user_func_array(array($obj, 'index'), $params);

Your code basically does:

Example::index($params);

which calls index statically, which you correctly assumed.

Sign up to request clarification or add additional context in comments.

2 Comments

Forgot to mention I'm making use of autoloading sorry. I was under the impression that call_user_func_array would would without having to instantiate the class manually. Is this not the case?
@Riddian Nope. It's not. There would be no way to actually perform static calls. BTW: this behavior goes for all PHP functions taking callbacks (like usort and the likes). See Pseudo-types and variables....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.