call_user_func_array 總結

If you need to call just function with parameters:
call_user_func_array('Foo',$args);

If you need to call CLASS method (NOT object):
call_user_func_array(array('class', 'Foo'),$args);

If you need to call OBJECT method:
call_user_func_array(array(&$Object, 'Foo'),$args);

If you need to call method of object of object:
call_user_func_array(array(&$Object->Object, 'Foo'),$args);

If you need to call object method from within the very same object (NOT CLASS!):
call_user_func_array(array(&$this, 'Foo'),args);

實例:

class Test {
public $func;

function callback($var) {
echo $var;
}
}

class Test1 {
public $prop;

public function test($var) {
echo $var;
}
}

$obj = new Test();
call_user_func_array(array(&$obj, 'callback'), array(1));

$obj->func = new Test1();
call_user_func_array(array(&$obj->func, 'test'), array(11));

總結二:
$args = func_get_args();
call_user_func_array(array('parent', '__construct'), $args);
子類可以調用父類而不必在意參數

實例
error_reporting('E_STRICT');
class A {
public function __construct($aa, $bb, $cc, $dd) {
echo $aa.$bb;
}
}

class B extends A {
public function __construct() {

$args = func_get_args();
call_user_func_array(array('parent', '__construct'), $args);

//parent::__construct($aa, $bb);
}
}

$obj = new B(1, 2);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章