How to call a function from a string stored in a variable?

后端 未结 16 2299
予麋鹿
予麋鹿 2020-11-22 10:16

I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:

function foo ()
{
  //code here
}

function bar ()
{
          


        
相关标签:
16条回答
  • 2020-11-22 10:54

    Use the call_user_func function.

    0 讨论(0)
  • 2020-11-22 10:56

    In case someone else is brought here by google because they were trying to use a variable for a method within a class, the below is a code sample which will actually work. None of the above worked for my situation. The key difference is the & in the declaration of $c = & new... and &$c being passed in call_user_func.

    My specific case is when implementing someone's code having to do with colors and two member methods lighten() and darken() from the csscolor.php class. For whatever reason, I wanted to have the same code be able to call lighten or darken rather than select it out with logic. This may be the result of my stubbornness to not just use if-else or to change the code calling this method.

    $lightdark="lighten"; // or optionally can be darken
    $color="fcc";   // a hex color
    $percent=0.15;  
    include_once("csscolor.php");
    $c = & new CSS_Color($color);
    $rtn=call_user_func( array(&$c,$lightdark),$color,$percent);
    

    Note that trying anything with $c->{...} didn't work. Upon perusing the reader-contributed content at the bottom of php.net's page on call_user_func, I was able to piece together the above. Also, note that $params as an array didn't work for me:

    // This doesn't work:
    $params=Array($color,$percent);
    $rtn=call_user_func( array(&$c,$lightdark),$params);
    

    This above attempt would give a warning about the method expecting a 2nd argument (percent).

    0 讨论(0)
  • 2020-11-22 10:56

    Following code can help to write dynamic function in PHP. now the function name can be dynamically change by variable '$current_page'.

    $current_page = 'home_page';
    $function = @${$current_page . '_page_versions'};
    $function = function() {
        echo 'current page';
    };
    $function();
    
    0 讨论(0)
  • 2020-11-22 10:58

    $functionName() or call_user_func($functionName)

    0 讨论(0)
  • 2020-11-22 10:58

    My favorite version is the inline version:

    ${"variableName"} = 12;
    
    $className->{"propertyName"};
    $className->{"methodName"}();
    
    StaticClass::${"propertyName"};
    StaticClass::{"methodName"}();
    

    You can place variables or expressions inside the brackets too!

    0 讨论(0)
  • 2020-11-22 11:01

    Complementing the answer of @Chris K if you want to call an object's method, you can call it using a single variable with the help of a closure:

    function get_method($object, $method){
        return function() use($object, $method){
            $args = func_get_args();
            return call_user_func_array(array($object, $method), $args);           
        };
    }
    
    class test{        
    
        function echo_this($text){
            echo $text;
        }
    }
    
    $test = new test();
    $echo = get_method($test, 'echo_this');
    $echo('Hello');  //Output is "Hello"
    

    I posted another example here

    0 讨论(0)
提交回复
热议问题