Php& #39;s call_user_func with variable不起作用


php's call_user_func with variable do not work

我尝试动态调用对象的方法,但没有得到它的工作

the not working case
$method = 'my_function';
$target_xml = call_user_func(array($api, $method));
Warning: call_user_func() expects parameter 1 to be a valid callback, second array member is not a valid method 

如果我var_dump(array($api, $method))

array (size=2)
  0 => 
    object(APIModel)[12]
      private 'client' => null
      private 'affiliate' => null
      private 'prod_category' => null
      protected 'viewModel' => 
        object(ViewModel)[13]
      protected 'db' => 
        object(MysqliDb)[14]
          protected '_mysqli' => 
            object(mysqli)[15]
              public 'affected_rows' => null
              public 'client_info' => null
              public 'client_version' => null
              public 'connect_errno' => null
              public 'connect_error' => null
              public 'errno' => null
              public 'error' => null
              public 'error_list' => null
              public 'field_count' => null
              public 'host_info' => null
              public 'info' => null
              public 'insert_id' => null
              public 'server_info' => null
              public 'server_version' => null
              public 'stat' => null
              public 'sqlstate' => null
              public 'protocol_version' => null
              public 'thread_id' => null
              public 'warning_count' => null
          protected '_query' => null
          protected '_join' => 
            array (size=0)
              empty
          protected '_where' => 
            array (size=0)
              empty
          protected '_whereTypeList' => null
          protected '_orderBy' => 
            array (size=0)
              empty
          protected '_groupBy' => 
            array (size=0)
              empty
          protected '_paramTypeList' => null
          protected '_bindParams' => 
            array (size=1)
              0 => string '' (length=0)
  1 => 
    object(SimpleXMLElement)[16]
      string 'my_function' (length=5)

工作情况下

$target_xml = call_user_func(array($api, 'my_function'));

正确用法:

$target_xml = call_user_func('my_function', array($api));