php call_user_func_array用法

实例

<?php
function func($a,$b)
{
    echo "a:" . $a ."<br>";
    echo "b:" . $b;
}
//直接方法名调用
call_user_func_array('func', [1,2]);
echo "<br>";

class MyClass{

    public function func($arg){
        echo 'MyClass func ' . $arg;
    }

    public static function staticFunc($arg){
        echo "Mysql static func " . $arg;
    }
}
//调用类里的普通方法和静态方法
$myClass = new MyClass();
call_user_func_array([$myClass,"func"],['huyongjian']);
echo "<br>";
call_user_func_array(["MyClass", "staticFunc"], ['张三']);
echo "<br>";

class MyClass2{
    function getName($username,$callback){
        return call_user_func_array($callback,[$username]);
    }

    function upper($username){
        return strtoupper($username);
    }
}
//类里方法里调用
$myClass2 = new MyClass2();
echo $myClass2->getName("huyongjian",[$myClass2,"upper"]);

结果显示

a:1
b:2
MyClass func huyongjian
Mysql static func 张三
HUYONGJIAN
上一篇:【GAMES101-现代计算机图形学课程笔记】Lecture 01 Overview of Computer Graphics


下一篇:apiCode/1/1.1/1.1.1