设为首页 收藏本站
查看: 578|回复: 0

[经验分享] PHP函数处理函数实例详解

[复制链接]

尚未签到

发表于 2015-8-27 08:58:42 | 显示全部楼层 |阅读模式
  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 3
The 0th arg is hello
The 1th arg is world
The 2th arg is 123456
-------------------------------------------
hello
world
123456
  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-104777-1-1.html 上篇帖子: PHP-PDO 下篇帖子: PHP使用PDO连接Access
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表