Cara menggunakan is_callable php 8

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The is_callable() function is an inbuilt function in PHP which is used to verify the contents of a variable can be called as a function. It can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name.

    Syntax:

    bool is_callable ( $variable_name, $syntax_only, $callable_name )

    Parameters: The is_callable() function accepts three parameters as shown in above syntax and are described below. It depends on user to use how many parameters one, two or three.

    • $variable_name: The name of a function stored in a string variable $variable_name, or an object and the name of a method within the object.
    • $syntax_only: If set to TRUE the function only verifies that name might be a function or method. It will reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback. The valid ones are supposed to have only 2 entries, the first of which is an object or a string, and the second a string.
    • $callable_name: Receives the callable name. This option only implemented for classes.

    Return value: This function returns a boolean type value. It returns TRUE if $variable_name is callable, FALSE otherwise.

    Below program illustrate the is_callable() function in PHP:
    Program 1: Simple variable contains a function

    <?php

    function Function_xyz() 

    {

    }

    $variable_name = "Function_xyz";

    var_dump(is_callable($variable_name, false, $callable_name));

    echo $callable_name, "\n";

    var_dump(is_callable($variable_name));

    ?>

    Output:

    bool(true)
    Function_xyz
    bool(true)
    

    Program 2: Array contains a method

    <?php

    class ClassA {

      function Method_xyz() 

      {

      }

    }

    $obj = new ClassA();

    $variable_name = array($obj, 'Method_xyz');

    var_dump(is_callable($variable_name, true, $callable_name));  

    echo $callable_name, "\n";  

    ?>

    Output:

    bool(true)
    ClassA::Method_xyz
    

    Reference: http://php.net/manual/en/function.is-callable.php


    LOL I figured that would get your attention, what the hell are you doing Spuds.

    Its the dispatcher, the hook processor and the special front page hook areas. All of the other uses that I've seen are using the call on an object, not a string. I changed the other areas to use the get_class_methods() call and its working as expected. But to your point in 2.0 I think we could rework that and gain some efficiency. Its pretty protective now and that actually made finding the problem difficult since it did not toss or log any error.

    The only other 8.0 issue of note is in the event manager. PHP 8 will stop dead if you pass parameters to a method that accepts none (and presumably wrongly named / ordered ones). So I did a little dance with

    				// Lets check on the required method parameters
    				try
    				{
    					$r = new ReflectionMethod($class_name, $method_name);
    					$number_params = $r->getNumberOfParameters();
    					unset($r);
    				}
    				catch (\Exception $e)
    				{
    					$number_params = 0;
    				}
    
                                   if (empty($dependencies) || empty($number_params))
                                         bla bla bla
    

    Which will ensure no parameters are sent to a method that uses none. Dirty but seemed like an easy fix. In 2.0 could use this to ensure the parameter names match what is expected, log errors, etc etc

    Anyway have to run to the store now, my neighbor has the old corona-mona and needs some stuff so off I go!