function_exists fatal error

Posted on

Question :

Hello, I need help solving this problem.

I have the following code snippet (I put the line number to facilitate):

**186** if ( function_exists( self::$function_val() ) === TRUE ){
**187**    call_user_func( self::$function_val() );
**188** }

But the following error appears on the screen:


Fatal error: Call to undefined method Widgets :: contacts () in /var/www/html/ui/includes/class_widgets.php on line 186

The function Widgets :: contacts () does not really exist, but the purpose of function_exists is not to verify this and only return TRUE or FALSE ??


Answer :

If you are going to work with (class properties) methods, the best way is to use method_exists instead of function_exists .


class A
    static function mostra(){

    private function esconde(){

    public function existe($metodo){
        if(method_exists($this, $metodo)){
            print "existe: Existe<br>";
        } else {
            print "existe: Nao existe<br>";

    public function existe_metodo($metodo){
            print "existe_metodo: Existe<br>";
        } else {
            print "existe_metodo: Não existe<br>";

$p = new A;


On the other hand:

function qualquer(){

    print "Sou a funcao 'qualquer' e existo<br>";

In your example, what happens is that you are looking for a non-existent method, it will always return error, because you are using the% scope_operator operator, this not only checks, it also tries to access the method itself, hence the undefined method . The correct one would be to use :: as in the examples, giving the object name and the method you want to check whether it is static or not.


  • function_exists – looks only at the list of user-defined and built-in functions.

  • method_exists – search for the method based on the object indicated in the first parameter.


You’re confusing some things. The function function_exists gets a string which would be the name of the function to be able to evaluate if a function with that name exists or not.

In your case, when you are calling self::$function_val() you are asking PHP to dynamically call a static method present in self , however using a variable value present in $function_val .

That is:

$function_val = 'method';


It would be equivalent to:


In your example, it is how you were invoking the method to bring the result to be checked with function_exists , but you can see that this was not your intention.

To check for a method to call it, you should use method_exists .

if (method_exists(get_called_class(), $function_val)) {


If you are using PHP 5.5 or higher, you can switch get_called_class() to static::class or self::class .


If self::$function_val is receiving a string with the name of a function and I believe that call_user_func is unnecessary (actually wrong), doing this self::$function_val() you are already calling the variable with function e then call_user_func is trying to execute the function return and not the function

Then do this ( === TRUE is really redundant I’ve removed):

if ( function_exists( self::$function_val ) ){

If you’re getting stuff like methods, closures, functions, you can use is_callable :

if ( is_callable( self::$function_val ) ){

Maybe call self::$function_val() actually returns another function you put back call_user_func .


The function “function_exists” should be given the function name as string, in your example you are passing the function as a parameter.

if ( function_exists( 'nome_da_funcao' ) ) {
     call_user_func( self::$function_val() );


Leave a Reply

Your email address will not be published. Required fields are marked *