dongyou2305 2016-12-14 03:01
浏览 152
已采纳

php call_user_func变量错误

I'm just feeling my way around with some tests to get an idea of how to tackle a coding issue. Now, I'm getting behavior I don't understand.

function product($a, $b) {
echo "Yo ".$a;
echo "Yo ".$b;
}

call_user_func('product', 'dd', 'lizzy');

The above works, however,

$variablename = "'product','dd','lizzy'";
call_user_func($variablename);

throws an error:

Warning: call_user_func() expects parameter 1 to be a valid callback, function ''product','dd','lizzy'' not found or invalid function name

Obviously $variablename contains the same values.

Anyone any hint for me to put me back on track?

  • 写回答

4条回答 默认 最新

  • douyin4875 2016-12-14 03:53
    关注

    Pay attention to the signature of call_user_func and call_user_func_array.

    function product($a, $b) {
      echo "Yo ".$a;
      echo "Yo ".$b;
      echo "<br>
    ";
    }
    
    // there are 3 single arguments
    call_user_func('product', 'dd', 'lizzy');
    
    // the same with variables
    $fn_name = 'product';
    $arg1    = 'dd';
    $arg2    = 'lizzy';
    
    call_user_func($fn_name, $arg1, $arg2);
    
    // or give ARGUMENS ONLY as array, but function name as string scalar
    $args    = array($arg1, $arg2);
    call_user_func_array($fn_name, $args);
    
    // if you for some reason need to handle a single array for the name and arguments in one,
    // you need to write your own function:
    function call_user_func_array2($call_info)
    {
      $fn_name = array_shift($call_info);  //extract first item
      call_user_func_array($fn_name, $call_info);
    }
    
    $call_info = array($fn_name, $arg1, $arg2);
    call_user_func_array2($call_info);
    

    In addition we can extend the code above to process on a given string "'product','dd','lizzy'". (e.g. in situations when it is stored in this form in some database)

    You first need to extract the comma separated values in this string. You can achieve this by str_getcsv or more flexible by a regular expression. Let's say it is separated by comma with optional whitespaces and enclosed in single or double qoutes. The regex could look like: (?:^|,)\s*(['"])(.+?)\1\s*?(?=,|$). The 2nd subpatter (.+?) will capture everything inside the single/double quotes.

    Combined with call_user_func_array2 it looks like that:

    function call_user_func_string($call_info)
    {
      if(!preg_match_all('~(?:^|,)\s*([\'"])(.+?)\1\s*?(?=,|$)~u', $call_info, $matches))
        throw new Exception("call_user_func_string expects parameter 1 to be a string like `'functionname','arg1','arg2' [,...]` "
                          . 'or `"functionname","arg1","arg2" [,...]`.');
    
      call_user_func_array(array_shift($matches[2]), $matches[2]);
    }
    
    
    // some differently formatted examples
    
    $my_string = "'product','dd','lizzy'";
    call_user_func_string($my_string);
    
    $my_string = '  "product"  ,  "dd"  ,  "lizzy"  ';
    call_user_func_string($my_string);
    
    $my_string = <<<'_END_'
        'product'   ,   "dd",'lizzy'    ,"lucy's cat"   
    _END_;
    
    call_user_func_string($my_string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料