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

后端 未结 16 2520
予麋鹿
予麋鹿 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: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!

提交回复
热议问题