2008-11-07 63 views

回答

80

這兩種方式都可以 - 你需要使用正確的語法

// Non static call 
call_user_func(array($obj, 'method')); 

// Static calls 
call_user_func(array('ClassName', 'method')); 
call_user_func('ClassName::method'); // (As of PHP 5.2.3) 
+3

而這些有用: http://stackoverflow.com/questions/251485/dynamic-class-method-invocation-in-php – 2014-05-03 20:40:59

16

你的意思是這樣嗎?

<?php 

class A { 
    function test() { 
     print 'test'; 
    } 
} 

$function = 'test'; 

// method 1 
A::$function(); 

// method 2 
$a = new A;  
$a->$function(); 

?> 
16

選項1

// invoke an instance method 
$instance = new Instance(); 
$instanceMethod = 'bar'; 
$instance->$instanceMethod(); 

// invoke a static method 
$class = 'NameOfTheClass'; 
$staticMethod = 'blah'; 
$class::$staticMethod(); 

選項2

// invoke an instance method 
$instance = new Instance(); 
call_user_func(array($instance, 'method')); 

// invoke a static method 
$class = 'NameOfTheClass'; 
call_user_func(array($class, 'nameOfStaticMethod')); 
call_user_func('NameOfTheClass::nameOfStaticMethod'); // (As of PHP 5.2.3) 

選項1比選項2快,故嘗試使用它們,除非你不知道你要去多少個參數是傳遞給該方法。


編輯:上一頁編輯器做清理我的回答的非常出色,但除去call_user_func_array提到這是不同的,那麼call_user_func。

PHP有

mixed call_user_func (callable $callback [, mixed $parameter [, mixed $... ]]) 

http://php.net/manual/en/function.call-user-func.php

AND

mixed call_user_func_array (callable $callback , array $param_arr) 

http://php.net/manual/en/function.call-user-func-array.php

使用call_user_func_array是大小慢然後使用上面列出任一選項的訂單。

1

編輯:我剛剛解決了你正在試圖問的問題......好吧..會留下我的意見。如果你喜歡,你可以替換的類和方法的名稱與變量。(但你是瘋了) - 尼克


要從類中調用一個函數,你可以做到這一點的兩種方法之一...

您可以創建該類的一個實例,然後調用它。 例如: -

$bla = new Blahh_class(); 
$bla->do_something(); 

或...你可以靜態調用函數..即沒有類的實例。 例如:

Blahh_class::do_something() 
當然

你需要聲明你的函數是靜態的:

class Blahh_class { 
    public static function do_something(){ 
     echo 'I am doing something'; 
    } 
} 

如果一個類不被定義爲靜態的,那麼你必須創建對象的實例。(所以對象需要一個構造函數) 如:

class Blahh_class { 
    $some_value; 

    public function __construct($data) { 
     $this->$some_value = $data; 
    } 

    public function do_something() { 
     echo $this->some_value; 
    } 
} 

要記住的重要一點是,靜態類的功能不能使用$this因爲沒有了CLAS實例秒。 (這就是爲什麼他們去得更快的原因之一。)

0

我覺得最好的辦法是做

call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip')); 

它的工作就像一個魅力!

0
Class Foo{ 
public function show(){ 
    echo 'I am in Foo Class show method'; 
} 

} 

call_user_func(array('Foo', 'show')); 

$classname = new Foo; 
call_user_func(array($classname, 'show')); 
call_user_func($classname .'::show'); // As of 5.2.3 

$foo = new Foo();  
call_user_func(array($foo, 'show')); 
0

這可能是作爲替代

class ReferenceContainer { 

    function __construct(CallbackContainer $callbackContainer) { 

     //Alternatively you can have no parameters in this constructor and create a new instance of CallbackContainer and invoke the callback in the same manner   
     //var_dump($this->callbackContainer); 
     $data = 'This is how you parse a class by reference'; 
     $callbackContainer->myCallback($data); 

    } 

} 

class CallbackContainer { 

    function __construct() {} 

    function myCallback($data) { 

     echo $data."\n"; 

    } 

} 

$callbackContainer = new CallbackContainer(); 
$doItContainer = new ReferenceContainer($callbackContainer);