php 回调函数 call_user_func_array 的简单使用

call_user_func_array 回调函数有几种常用的回调

  • 全局函数的回调
  • 类的静态方法的回调

全局函数的回调

	<?php
	/**
	 * Created by PhpStorm.
	 * User=> binWei
	 */


	function fnCallBack($msg1, $msg2)
	{
			echo 'msg1:' . $msg1;
			echo "<br />\n";
			echo 'msg2:' . $msg2;
	}

	$params = array('hello', 'world');
	call_user_func_array('fnCallBack', $params);

结果:
file

类的静态方法的回调

<?php
/**
 * Created by PhpStorm.
 * User: v_bivwei
 * Date: 2019/10/15
 * Time: 15:04
 */

class MyClass
{
    public static function fnCallBack($msg1, $msg2)
    {
        echo 'msg1:' . $msg1;
        echo "<br />\n";
        echo 'msg2:' . $msg2;
        return "is class static function";
    }
}

$className = 'MyClass';
$fnName = "fnCallBack";
$params = array('class==hello', 'class=world');
$ret = call_user_func_array(array($className, $fnName), $params);
var_dump($ret);

结果:
file
对于类的非静态方法也可以使用这种方式。
不建议用 call_user_func_array 来回调一个对象的方法。

发布了145 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xiaobinqt/article/details/102573034