1. call_user_func和call_user_func_array: 

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

<?php
class 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"));

运行结果如下:

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表示第一个参数。

<?php
function 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');

运行结果如下:

Stephens-Air:TestPhp$ php class_exist_test.php
The number of args in myTest is
The 0th arg is hello
The 1th arg is world
The 2th arg is -------------------------------------------
hello
world

3. function_exists和register_shutdown_function:

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

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

<?php
function 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";

运行结果如下:

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

PHP函数处理函数实例详解的更多相关文章

  1. Vue钩子函数生命周期实例详解

    vue生命周期简介 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁 ...

  2. Oracle排名函数(Rank)实例详解

    这篇文章主要介绍了Oracle排名函数(Rank)实例详解,需要的朋友可以参考下     --已知:两种排名方式(分区和不分区):使用和不使用partition --两种计算方式(连续,不连续),对应 ...

  3. 这个贴子的内容值得好好学习--实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化

    感觉要DJANGO用得好,ORM必须要学好,不管理是内置的,还是第三方的ORM. 最最后还是要到SQL.....:( 这一关,慢慢练啦.. 实例详解Django的 select_related 和 p ...

  4. PHP用strtotime()函数比较两个时间的大小实例详解

    在PHP开发中,我们经常会对两个时间的大小进行判断,但是,在PHP中,两个时间是不可以直接进行比较,因为时间是由年.月.日.时.分.秒组成的,所以,如果需要将两个时间进行比较的话,我们首先要做的就是将 ...

  5. PHP函数call_user_func和call_user_func_array详解

    今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的: call_user_func_array (P ...

  6. JS函数动作分层结构详解及Document.getElementById 释义 js及cs数据类型区别 事件 函数 变量 script标签 var function

    html +css 静态页面 js     动态 交互   原理: js就是修改样式, 比如弹出一个对话框. 弹出的过程就是这个框由disable 变成display:enable. 又或者当鼠标指向 ...

  7. WordPress函数:get_bloginfo()用法详解

    描述 返回你博客的信息,这些信息可以用在任何地方的 PHP 代码中.这个函数,和 bloginfo() 一样,可以用来在模板文件的任何地方显示你博客的信息. 用法 <?php $bloginfo ...

  8. Wordpress菜单函数wp_nav_menu各参数详解及示例

    Wordpress菜单函数wp_nav_menu各参数详解及示例   注册菜单 首先要注册菜单,将以下函数添加至function.php函数里   register_nav_menus(array( ...

  9. Delphi Format函数功能及用法详解

    DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...

  10. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

随机推荐

  1. redis/php redis扩展 安装

    作者:silenceper 日期:2013-10-03 原文地址: http://silenceper.com/archives/952.html 我是在CentOS 6.3 中进行的. 使用到的软件 ...

  2. cocos2d-x 2.2.2 android平台移植

    1.完成以上工具的下载安装--cocos2d-x 2.2.2 --eclipse+adt+sdk --ndk 2.创建cocos2d-x工程 在"cocos2d-x-2.2.2\tools\ ...

  3. 12G服务器在BIOS中收集阵列卡日志(TTY日志)的方法

      如果系统进不去.请参考如下方法收集日志. 请准备个U 盘,容量在8G以下(含8G),否则会识别不到. 图片参考,以描述为准 F2 enter BIOS option--> Enter the ...

  4. Tomcat相关的笔记

    本文只是记录一下tomcat运维用到过的知识,都是网络上收集来的资料,侵删 JVM的内存 内存分配 -XX:PermSize尽量比-XX:MaxPermSize小,-XX:MaxPermSize> ...

  5. 位运算(&)实现分享弹窗上的图标动态显示/隐藏

    一  需求     要求自定义弹窗,上面动态显示多种分享平台,根据后台api接口传递过来的type控制显示哪些平台icon     1 定义平台变量,用2的几次方来定value     2 若要显示那 ...

  6. 《C#图解教程》读书笔记之二:存储、类型和变量

    本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.类型初窥:掀起你的盖头来 (1)C程序是一组函数和数据类型,C++程序是一组函数和类,而C#程序是一组类型声 ...

  7. C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)

    上一篇学习完了MEF的基础知识,编写了一个简单的DEMO,接下来接着上篇的内容继续学习,如果没有看过上一篇的内容, 请阅读:http://www.cnblogs.com/yunfeifei/p/392 ...

  8. Coding Kata - 挑战你的“底线”

    Coding Kata简介 如何进行Kata练习 亲身感受 Coding Kata简介 前段时间听到一个比较有意思的概念叫做Coding Kata,今天试了一下来说说一些想法和思考.Kata是一个日语 ...

  9. 跟我一起云计算(6)——openAPI

    介绍 Open API即开放API,也称开放平台. 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网站服务封装成一系列API(Application Program ...

  10. 强大的Sublime编辑器

    Sublime是一款功能非常强大的轻量级的代码及文本编辑工具,有关它的介绍和下载可以从官网http://www.sublimetext.com获取.尽管Sublime并非是一款完全免费的IDE开发工具 ...