call_user_func()详解

call_user_func()详解

 

先来看解释 :

(PHP 4, PHP 5, PHP 7)

call_user_func — 把第一个参数作为回调函数调用

通过函数的方式回调
<?php 
function barber($type){
    echo "you wanted a $type haircut, no problem\n";
}
call_user_func('barber','mushroom');
 
 
 ?>

返回内容如下:
you wanted a mushroom haircut, no problem

通过类名、对象的方式回调

<?php 
/**
 * 用call_user_func()来调用一个类里面的方法
 */
 
class myclass{
    static function say_hello(){
        echo "hello!\n";
    }
}
 
$classname = "myclass";
 
//通过数组键值的方式,对类名进行回调,回调类名里面的,say_hello方法
call_user_func(array($classname,'say_hello'));
 
//通过类名直接调用静态方法
call_user_func($classname .'::say_hello'); // As of 5.2.3// $myobject = new myclass();
 
//通过对象的方式回调
$myobject = new myclass();
call_user_func(array($myobject, 'say_hello'));
 
 ?>


通过$this关键字进行对类的回调,以下源码出自thinkphp5 controller.php   200行

call_user_func([$this, $method]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值