php convert string to method call

后端 未结 3 1594
臣服心动
臣服心动 2021-01-15 08:25


        
相关标签:
3条回答
  • 2021-01-15 08:40

    You can use the variable as a function name. This will execute getList():

    $str();
    

    However, stuff like this is mostly a symptom of a design problem. Care to elaborate what you need this for?

    0 讨论(0)
  • 2021-01-15 08:45

    Use the call_user_func() function to call the function by name.

    0 讨论(0)
  • 2021-01-15 08:47

    This feature is known as Variable functions, here is an example from php.net:

     <?php
     function foo() {
         echo "In foo()<br />\n";
     }
    
     function bar($arg = '')
     {
         echo "In bar(); argument was '$arg'.<br />\n";
     }
    
     // This is a wrapper function around echo
     function echoit($string)
     {
         echo $string;
     }
    
     $func = 'foo';
     $func();        // This calls foo()
    
     $func = 'bar';
     $func('test');  // This calls bar()
    
     $func = 'echoit';
     $func('test');  // This calls echoit()
     ?>
    

    More Info:

    • http://php.net/manual/en/functions.variable-functions.php
    0 讨论(0)
提交回复
热议问题