call_user_func(array(self, 'method')) - do I have to name the class?

If you want the name of the current class context, use get_class() (without any parameters) or __CLASS__.

You've already written the difference; self is a keyword, and is not usable as a reference in an array (what kind of type should that be in PHP?). get_class() returns a string, and the array()-callback supports using a string as the first name to do a static call.


You can try with __CLASS__ to get the class name. Also it may work to use call_user_func('self::method_name') directly, but I didn't test it and the documentation about callbacks doesn't say anything about this.


self is just an undefined constant, so it expresses 'self'. So these two are the same:

array(self, 'method_name');
array('self', 'method_name');

And depending on the PHP version you use, this actually works, see Demo.

In case it does not work with your PHP version, some alternatives are:

call_user_func(array(__CLASS__, 'method_name'));

or

call_user_func(__CLASS__.'::method_name'));

or (in case you don't need call_user_func):

self::method_name();

Tags:

Php

Oop