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

后端 未结 16 2431
予麋鹿
予麋鹿 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 11:02

    The easiest way to call a function safely using the name stored in a variable is,

    //I want to call method deploy that is stored in functionname 
    $functionname = 'deploy';
    
    $retVal = {$functionname}('parameters');
    

    I have used like below to create migration tables in Laravel dynamically,

    foreach(App\Test::$columns as $name => $column){
            $table->{$column[0]}($name);
    }
    

提交回复
热议问题