function_exists fatal error

4

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 ??

    
asked by anonymous 03.04.2017 / 16:01

4 answers

4

If you are going to work with (class properties) methods, the best way 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, 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.
  •   
    
03.04.2017 / 20:39
1

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';

Test::$function_val();

It would be equivalent to:

 Test::method();

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 .

    
07.04.2017 / 01:53
0

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 ) ){
    self::$function_val();
}

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

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

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

    
07.04.2017 / 01:00
-2

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() );
}
    
03.04.2017 / 20:07