php 自定义函数大全
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 自定义函数大全的更多相关文章
- python自定义函数大全
写的零碎的python脚本太多了,到一定阶段就会出现一个问题,即以前写过的脚本找不到了,现在临时要用,还得再重写一遍,这就非常难受了,代码不能复用. 还好我有一个比较好的习惯,我喜欢把python脚本 ...
- SQL SERVER 函数大全[转]
SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...
- jquery 函数大全
jquery函数大全转载 Attribute:$(”p”).addClass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”te ...
- LoadRunner 函数大全之中文解释
LoadRunner 函数大全之中文解释 // sapgui_table_set_column_selected 模拟用户 // 单击表中的列标题. int sapgui_table_set_colu ...
- 【转载】SQL SERVER 函数大全
SQL Server 函数大全 一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下 ...
- Mysql - 存储过程/自定义函数
在数据库操作中, 尤其是碰到一些复杂一些的系统, 不可避免的, 会用到函数/自定义函数, 或者存储过程. 实际项目中, 自定义函数和存储过程是越少越好, 因为这个东西多了, 也是一个非常难以维护的地方 ...
- Entity Framework 6 Recipes 2nd Edition(10-5)译 -> 在存储模型中使用自定义函数
10-5. 在存储模型中使用自定义函数 问题 想在模型中使用自定义函数,而不是存储过程. 解决方案 假设我们数据库里有成员(members)和他们已经发送的信息(messages) 关系数据表,如Fi ...
- mysql 常用自定义函数解析
-- /* -- * 用于获取一记录数据,根据传入的分隔字符delim,索引位置pos,返回相对应的value -- * SELECT Json_getKeyValue({"A": ...
- mysql 自定义函数
原文:http://www.cnblogs.com/zhangminghui/p/4113160.html 引言 MySQL本身提供了内置函数,这些函数的存在给我们日常的开发和数据操作带来了很大的便利 ...
随机推荐
- Python3.x:sys.argv[]的简介
Python3.x:sys.argv[]的简介 sys模块通过sys.argv提供对任何命令行参数的访问.主要有两个参数变量: sys.argv是命令行参数的列表. len(sys.argv)是命令行 ...
- wamp server 3.0.0 修改默认浏览器,软件语言和配置文件编辑器
改默认IE浏览器为Chrome: wampmanager.conf : navigator ="C:\Program Files (x86)\Google\Chrome\Applicatio ...
- 安装tensorflow报ImportError: libcublas.so.9.0: cannot open shared object file的解决方法【转】
本文转载自:https://blog.csdn.net/qq_37274615/article/details/81099738 转载自:https://blog.csdn.net/qysh123/a ...
- 记录web api的request以及response(即写log)
https://www.cnblogs.com/felixnet/p/5689501.html https://blog.csdn.net/Vblegend_2013/article/details/ ...
- UVa 10328 Coin Toss(Java大数+递推)
https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具 ...
- 简单购物车的实现,session的使用
购物车浏览商品界面代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- iframe.contentWindow介绍
一.在使用iframe的页面,要操作这个iframe里面的DOM元素可以用: contentWindow.contentDocument(测试的时候chrome浏览器,要在服务器环境下) 1.先获取i ...
- 数据结构与算法(python版)教程
算法的性质 算法的描述 算法的设计与分析
- vue项目打包部署到nginx 服务器上
假如要实现的效果如下 http://ip/vue =>是进入首页访问的路径是 usr/local/nginx/html/vue http://ip/website =>是进 ...
- kue
尝试着看英文的文档,你发现他其实并不难. https://www.npmjs.com/package/kue