php和call_user_function

时间:2014-03-25 09:37:29

标签: php function

如果我有一组功能

private function create() { echo "create...."; }
private function read() { echo "read...."; }
private function edit() { echo "edit...."; }
..

以及如何使用

if(is_callable($this,"create")) {
    call_user_func(array($this, "createe"), 'Hello World');
} else {
    echo "not found";
} 

此外,如果我有一组静态函数

private static function create() { echo "create...."; }
private static function read() { echo "read...."; }
private static function edit() { echo "edit...."; }
..

如何使用此变体?

if(functon_exists("self::create")) {
    call_user_func(array($this, "createe"), 'Hello World');
} else {
    echo "not found";
} 

由于

1 个答案:

答案 0 :(得分:0)

对于静态功能,请使用:

call_user_func(array('NameOfClass', 'create'));

call_user_func(array(get_class(), 'create'));