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. Vim编程常用命令

    1.全文覆盖 程序发布到测试.开发环境后,经常需要远程登录Linux更改代码.平时在IDE中直接Ctrl+A.Ctrl+V覆盖整个文档,在vim中需要这样做 vim filename gg --跳到首 ...

  2. Java加密代码 转换成Net版

    java版本自己封装base64 package com.qhong; import java.io.UnsupportedEncodingException; import org.apache.c ...

  3. ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE

    https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf 1.You can host Web API inside IIS or ins ...

  4. python输出日期时间

    import datetime base = datetime.datetime.today() , ): print(base + datetime.timedelta(days=x))

  5. [sql]join的5种方式:inner join、left(outer) join、right (outer) Join、full(outer) join、cross join

    现在有两张表  如下图所示:             一 .inner join   返回的结果:两个表的交集行 二. left join   是left outer join的简写 返回结果:左表的 ...

  6. cf812B 搜索

    B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. HDU 1969 精度二分

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  8. link @import区别 src href的区别

    先说页面引入css的四种方式吧 1 在头部写在style里面 2 行内样式 tyle= 3 外部引入 link和@import的区别 link属于XHTML的标签,而@import只是css提供的一种 ...

  9. Java9新特性

    转载:http://blog.csdn.net/qq_32524177/article/details/77014757 写在前面的话:Java9来了,搜索了很多关于Java9的新特性,但文献不多,特 ...

  10. java读取PHP接口数据的实现方法(四)

    PHP文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...