function_exists fatal error

Asked

Viewed 100 times

4

Hello, I need help to solve this problem.

I have the following code snippet (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 Widgets::Contacts() function doesn’t really exist, but the purpose of function_exists is not to just check it and only return TRUE or FALSE??

  • He is saying that you are using an undefined method, not a function, so ta returning error.

  • What is the value of self::$function_val? It’s a string?

  • I have a feeling you’re confusing it with method_exists()

4 answers

4


If you’re gonna work with methods (class properties), the same ideal is to use method_exists instead of function_exists.

<?php

class A
{
    static function mostra(){
        return;
    }

    private function esconde(){
        return;
    }

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

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

$p = new A;

$p->existe('esconde');
$p->existe_metodo('esconde');

On the other hand:

function qualquer(){
    return;
}

if(function_exists('qualquer')){
    print "Sou a funcao 'qualquer' e existo<br>";
}

In your example, what happens is that you are looking for a non-existent method, will always return error, because you are using the operator ::(Scope Resolution Operator), this is not limited to checking, it also tries to access the method itself, hence the Undefined method. The right thing would be for you to use method_exists as in the examples, giving the name of the object and the method to be checked whether it is static or not.

  • function_exists - looks only in the list of functions defined by both user and internal.
  • method_exists - search for the method based on the object specified in the first parameter.

1

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

In your case, when you’re calling self::$function_val() is asking PHP to make a dynamic call from a static method present in self, but using a variable value present in $function_val.

That is to say:

$function_val = 'method';

Test::$function_val();

Would be equivalent to:

 Test::method();

In your example, it’s like you’re 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 the existence of a method to call you, you should use method_exists.

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

}

If you’re using PHP 5.5 or higher, you can switch get_called_class() for static::class or self::class.

0

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

So do this (the === TRUE really is redundant I removed):

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

If you are receiving various things like methods, closures, functions can use is_callable:

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

Chance the call self::$function_val() really return another function you put back the call_user_func.

-2

The function "function_exists" should take the name of the function as string, in your example you are passing the function as parameter.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.