zoukankan      html  css  js  c++  java
  • call_user_func 详解

    先来看解释 :

    (PHP 4, PHP 5, PHP 7)

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

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

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

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

    <?php 
    /**
     * 用call_user_func()来调用一个类里面的方法
     */
     
    class myclass{
        static function say_hello(){
            echo "hello! ";
        }
    }
     
    $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]);
    --------------------- 
    作者:Baymax001 
    来源:CSDN 
    原文:https://blog.csdn.net/Baymax001/article/details/72902579 
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    NUC_TeamTEST_C && POJ2299(只有归并)
    BestCoder#15 A-LOVE(暴力)
    NUC_TeamTEST_B(贪心)
    2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)
    CodeForces#275--DIV 2--A
    uva-1339Ancient Cipher
    uva748
    uva-465(overflow)
    uva10106(大数乘法)
    424
  • 原文地址:https://www.cnblogs.com/weixinsb/p/13226151.html
Copyright © 2011-2022 走看看