PHP函数处理函数实例详解

php中文网
发布: 2016-06-23 14:35:43
原创
1196人浏览过

1. call_user_func和call_user_func_array: 

    以上两个函数以不同的参数形式调用回调函数。见如下示例:  

 

<?phpclass AnotherTestClass {    public static function printMe() {        print "This is Test2::printSelf.\n";    }    public function doSomething() {        print "This is Test2::doSomething.\n";    }    public function doSomethingWithArgs($arg1, $arg2) {        print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").\n";    }}$testObj = new AnotherTestClass();call_user_func(array("AnotherTestClass", "printMe"));call_user_func(array($testObj, "doSomething"));call_user_func(array($testObj, "doSomethingWithArgs"),"hello","world");call_user_func_array(array($testObj, "doSomethingWithArgs"),array("hello2","world2"));
登录后复制

 

    运行结果如下:

立即学习PHP免费学习笔记(深入)”;

怪兽AI数字人
怪兽AI数字人

数字人短视频创作,数字人直播,实时驱动数字人

怪兽AI数字人 44
查看详情 怪兽AI数字人

bogon:TestPhp$ php call_user_func_test.php This is Test2::printSelf.This is Test2::doSomething.This is Test2::doSomethingWithArgs with ($arg1 = hello and $arg2 = world).This is Test2::doSomethingWithArgs with ($arg1 = hello2 and $arg2 = world2).
登录后复制

2. func_get_args、func_num_args和func_get_args: 

    这三个函数的共同特征是都很自定义函数参数相关,而且均只能在函数内使用,比较适用于可变参数的自定义函数。他们的函数原型和简要说明如下:
int func_num_args (void) 获取当前函数的参数数量。
array func_get_args (void) 以数组的形式返回当前函数的所有参数。
mixed func_get_arg (int $arg_num) 返回当前函数指定位置的参数,其中0表示第一个参数。

<?phpfunction myTest() {    $numOfArgs = func_num_args();    $args = func_get_args();    print "The number of args in myTest is ".$numOfArgs."\n";    for ($i = 0; $i < $numOfArgs; $i++) {        print "The {$i}th arg is ".func_get_arg($i)."\n";    }    print "\n-------------------------------------------\n";    foreach ($args as $key => $arg) {        print "$arg\n";    }}myTest('hello','world','123456');
登录后复制

    运行结果如下:

立即学习PHP免费学习笔记(深入)”;

Stephens-Air:TestPhp$ php class_exist_test.php The number of args in myTest is 3The 0th arg is helloThe 1th arg is worldThe 2th arg is 123456-------------------------------------------helloworld123456
登录后复制

3. function_exists和register_shutdown_function:

    函数原型和简要说明如下:

 

bool function_exists (string $function_name) 判断某个函数是否存在。
void register_shutdown_function (callable $callback [, mixed $parameter [, mixed $... ]]) 在脚本结束之前或者是中途调用exit时调用改注册的函数。另外,如果注册多个函数,那么他们将会按照注册时的顺序被依次执行,如果其中某个回调函数内部调用了exit(),那么脚本将立刻退出,剩余的回调均不会被执行。

 

<?phpfunction shutdown($arg1,$arg2) {    print '$arg1 = '.$arg1.', $arg2 = '.$arg2."\n";}if (function_exists('shutdown')) {    print "shutdown function exists now.\n";}register_shutdown_function('shutdown','Hello','World');print "This test is executed.\n";exit();print "This comments cannot be output.\n";
登录后复制

    运行结果如下:

立即学习PHP免费学习笔记(深入)”;

Stephens-Air:TestPhp$ php call_user_func_test.php shutdown function exists now.This test is executed.$arg1 = Hello, $arg2 = World
登录后复制

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号