2014-03-27 15 views
-1

我嘗試動態調用對象的方法,但沒有得到它的工作PHP的可變call_user_func不工作

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(陣列($ API,$法))

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')); 
+1

是什麼'的var_dump(陣列($ API,$法));'告訴你?那裏可能還有一些額外的(不可打印的)角色? –

+0

'對象(的SimpleXMLElement)'似乎並不懷疑你...? – deceze

+0

是的,你是對的 – fefe

回答

-2

正確使用:

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

陣列括號 –

+0

這是不正確的用法。這是不正確:沒有一種方法指定 –

+1

的OP試圖'$ API - >創建my_function相當於()','不創建my_function(陣列($ API))'。 – deceze