1. call_user_func和call_user_func_array

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

class demo
{
public static function action1()
{
echo "This is demo::action1.<br>";
}

public function action2()
{
echo "This is demo::action2.<br>";
}

public function actionWithArgs($arg1, $arg2)
{
echo 'This is demo::actionWithArgs with ($arg1 = ' . $arg1 . ' and $arg2 = ' . $arg2 . ").<br>";
}
}

$demo = new demo();
call_user_func(array("demo", "action1"));
call_user_func(array($demo, "action2"));
call_user_func(array($demo, "actionWithArgs"), "hello", "world");
call_user_func_array(array($demo, "actionWithArgs"), array("hello2", "world2"));

运行结果如下:

This is demo::action1.
This is demo::action2.
This is demo::actionWithArgs with ($arg1 = hello and $arg2 = world).
This is demo::actionWithArgs 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 demoA()
{
$numOfArgs = func_num_args();
$args = func_get_args();
echo "The number of args in demoA is " . $numOfArgs . "<br>";
for ($i = 0; $i < $numOfArgs; $i++) {
echo "The {$i}th arg is " . func_get_arg($i) . "<br>";
}
echo "-------------------------------------------<br>";
foreach ($args as $arg) {
echo "$arg<br>";
}
}

demoA('hello', 'world', '123456');

运行结果如下:

The number of args in demoA is 3
The 0th arg is hello
The 1th arg is world
The 2th arg is 123456
-------------------------------------------
hello
world
123456

3. register_shutdown_function

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

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

<?php
function myShutdown($arg1, $arg2)
{
echo '$arg1 = ' . $arg1 . ', $arg2 = ' . $arg2 . "<br>";
}

if (function_exists('myShutdown')) {
print "myShutdown function exists now.<br>";
}

register_shutdown_function('myShutdown', 'Hello', 'World');
print "This test is executed.<br>";
exit();
echo "This comments cannot be output.<br>";

运行结果如下:

myShutdown function exists now.
This test is executed.
$arg1 = Hello, $arg2 = World

php 自定义函数大全的更多相关文章

  1. python自定义函数大全

    写的零碎的python脚本太多了,到一定阶段就会出现一个问题,即以前写过的脚本找不到了,现在临时要用,还得再重写一遍,这就非常难受了,代码不能复用. 还好我有一个比较好的习惯,我喜欢把python脚本 ...

  2. SQL SERVER 函数大全[转]

    SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...

  3. jquery 函数大全

    jquery函数大全转载  Attribute:$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”te ...

  4. LoadRunner 函数大全之中文解释

    LoadRunner 函数大全之中文解释 // sapgui_table_set_column_selected 模拟用户 // 单击表中的列标题. int sapgui_table_set_colu ...

  5. 【转载】SQL SERVER 函数大全

    SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...

  6. Mysql - 存储过程/自定义函数

    在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...

  7. Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数

    10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...

  8. mysql 常用自定义函数解析

    -- /* -- * 用于获取一记录数据,根据传入的分隔字符delim,索引位置pos,返回相对应的value -- * SELECT Json_getKeyValue({"A": ...

  9. mysql 自定义函数

    原文:http://www.cnblogs.com/zhangminghui/p/4113160.html 引言 MySQL本身提供了内置函数,这些函数的存在给我们日常的开发和数据操作带来了很大的便利 ...

随机推荐

  1. # 20145122 《Java程序设计》第3周学习总结

    教材学习内容总结 1一类一文件. 2一个原始码中只能有一个公开类,一个类定义产生一个.class文档. 3如果参考名称与数据成员同名时,将参数的值指定给对象的数据成员时要在数据成员前加this. 4当 ...

  2. iptable的使用

    目录 iptables iptables iptables详解 2012-07-18 20:10:08 分类: LINUX 一:前言 防火墙,其实说白了讲,就是用于实现Linux下访问控制的功能的,它 ...

  3. 如何把本地git仓库托管到码云上

    提交代码到本地git仓库 git init git status git add . git status git commit -m "init my project"     ...

  4. QT+VS中ui不能声明为指针?

    问题描述:QtCreator里的UI全是默认为指针类型,调用的时候[ui->]但是使用VS+Qt来,发来默认的是变量类型,使用的时候[ui.] 统一:为了统一我把后者声明改为前者 问题:在mai ...

  5. YouCompleteMe自动补全的安装配置与使用

    1 下载 git clone --recursive git://github.com/Valloric/YouCompleteMe 如果执行该命令没报错, 就ok了. 但是中途有可能会断掉, 可以 ...

  6. tar: Cowardly refusing to create an empty archive 问题

    在解压 .tar.gz文件的时候遇到了这个解压的问题. 原命令:tar -zcvf ···.tar.gz 提示:tar: Cowardly refusing to create an empty ar ...

  7. UVa 10054 项链(欧拉回路)

    https://vjudge.net/problem/UVA-10054 题意:有一种由彩色珠子连接成的项链.每个珠子的两半由不同颜色组成.相邻两个珠子在接触的地方颜色相同.现在有一些零碎的珠子,需要 ...

  8. spring boot: 输出json

    spring boot:  输出json 注意:关闭java的Terminate后,在重新启动,否则报错 app.java启动配置 package com.muyang.boot1; import o ...

  9. ObservableCollection<T> 的同类 ListCollectionView

    1:ListCollectionView : CollectionView : INotifyCollectionChanged, INotifyPropertyChanged  2:Observab ...

  10. gcd 与 扩gcd 总结

    gcd 定理的证明: 模板: ll gcd(ll a,ll b) { ) return a; else return gcd(b,a%b); } 扩gcd证明: 模板: ll extgcd(ll a, ...