Blogger Information
Blog 11
fans 0
comment 1
visits 15620
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php回调函数
JasonKim的博客
Original
1570 people have browsed it
<?php
/**
 * Created by PhpStorm.
 * User: Jason
 * Date: 2019/3/31
 * Time: 14:06
 */

// 普通函数
function bigger($a,$b)
{
    return "$a,$b 中较大的数是:".($a > $b ? $a : $b );
}

// 声明函数调用bigger
function main($big)
{
    return $big(12,32);
}
// 调用函数,把函数名称写入即可
echo main('bigger').'<hr>';


// 实际中是使用匿名函数完成函数注入
$bigger = function($a,$b)
{
    return "$a,$b 中较大的数是:".($a > $b ? $a : $b );
};
// 自定义函数
function main1($big) {
    return $big(23,52);
}
// 调用匿名函数
echo main1($bigger).'<hr>';


/**
 * php内置了两个双胞胎函数:用来执行回调
 * 1、call_user_func();  单个参数传参
 * 2、call_user_func_array(); 数组形式传参
 */

// 调用普通函数
echo call_user_func('bigger',233,532).'<hr>';

// 调用匿名函数
echo call_user_func($bigger,231,132).'<hr>';


// 工作中常用的是call_user_func_array();
echo call_user_func_array($bigger,[122,324]).'<hr/>';

// 类
class demos
{
    public static function getMethod()
    {
        return '方法是:'.__METHOD__;
    }

    public function getAge(){
        return 28;
    }
}

// 可以调用类中的方法
echo call_user_func(['demos','getMethod']).'<hr>';
echo call_user_func('demos::getMethod').'<hr>';
// 调用不到公有方法
echo call_user_func(['demos','getAge']);


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post