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. 二进制求最大公约数&&输出二进制

    Divided Land Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  2. 解决PHP move_uploaded_file函数移动图片失败

    出现的问题描述:今天在实现一个在用户注册时上传头像图片文件的PHP脚本时,出现了问题:PHP脚本在前面已经确定 浏览器端上传文件没有错误.上传的文件是合法的.上传的文件是图像文件.已经在服务器端生成了 ...

  3. ORACLE 自定义分页存储过程

    一.创建包 CREATE OR REPLACE PACKAGE PKG_JK_LAB_BASIC IS TYPE CURSOR_TYPE IS REF CURSOR; PROCEDURE SP_GET ...

  4. IIS部署Nodejs步骤

    需要iis的url重写插件 安装iisnode 配置文件 rewrite 节点需要url重写插件支持 node.exe 路径是你安装的路径 interceptor.js 是你安装iisnode的路径 ...

  5. 深入理解CSS弹性盒模型flex

    × 目录 [1]版本更迭 [2]display [3]基本概念[4]伸缩容器[5]伸缩项目 前面的话 CSS3引入了一种新的布局模型——flex布局.flex是flexible box的缩写,一般称之 ...

  6. Linux修改环境变量的方法

    在Linux操作系统中,有时候跟着教程安装了一些软件,安装成功后,很高兴的准备运行该软件相应命令,但是偶尔会遇到”Command not found…“的提示.原因是因为你安装的软件需要设置环境变量才 ...

  7. js只需5分钟创建一个跨三大平台纯原生APP

    DeviceOne之前介绍过了,现在来介绍一下DeviceOne快速开发到什么程度 使用js只需要5分钟就可以打出垮Android.ios.windows三大平台的纯原生UI的安装包. 只需要6个小时 ...

  8. Android学习笔记50:使用WebView控件浏览网页

    在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...

  9. swift NSComparator

    var cmptr:NSComparator = {(obj1:AnyObject!, obj2:AnyObject!) -> NSComparisonResult in if((obj1[&q ...

  10. 纯CSS实现3D按钮效果

    今天分享一个用纯CSS实现的3D按钮.css巧妙利用了box-shadow来实现3D物体的立体感,当按钮按下的时候再去修改box-shadow和top值.让人感觉有一种按钮被按下的感觉.css代码非常 ...