亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

PHP基礎之函數7——動態調用函數

瀏覽:4日期:2022-09-14 17:09:29
call_user_func

(PHP 4, PHP 5)

call_user_func?—?傳入一個參數來調用回調函數

說明mixed?call_user_func?(?callable?$callback?[,?mixed?$parameter?[,?mixed?$...?]] )

Calls the?callback?given by the first parameter and passes the remaining parameters as arguments.

參數callbackThe?callable?to be called.parameterZero or more parameters to be passed to the callback.

Note:

Note that the parameters for?call_user_func()?are not passed by reference.

Example #1?call_user_func()?example and references

<?phperror_reporting(E_ALL);function?increment(&$var){????$var++;}$a?=?0;call_user_func(’increment’,?$a);echo?$a.'n';call_user_func_array(’increment’,?array(&$a));?//?You?can?use?this?instead?before?PHP?5.3echo?$a.'n';?>

以上例程會輸出:

01?返回值

Returns the return value of the callback, or?FALSE?on error.

?范例

Example #2?call_user_func()?example

<?phpfunction?barber($type){????echo?'You?wanted?a?$type?haircut,?no?problemn';}call_user_func(’barber’,?'mushroom');call_user_func(’barber’,?'shave');?>

以上例程會輸出:

You wanted a mushroom haircut, no problemYou wanted a shave haircut, no problem

Example #3?call_user_func()?using namespace name

<?phpnamespace?Foobar;class?Foo?{????static?public?function?test()?{????????print?'Hello?world!n';????}}call_user_func(__NAMESPACE__?.’Foo::test’);?//?As?of?PHP?5.3.0call_user_func(array(__NAMESPACE__?.’Foo’,?’test’));?//?As?of?PHP?5.3.0?>

以上例程會輸出:

Hello world!Hello world!

Example #4 Using a class method with?call_user_func()

<?phpclass?myclass?{????static?function?say_hello()????{????????echo?'Hello!n';????}}$classname?=?'myclass';call_user_func(array($classname,?’say_hello’));call_user_func($classname?.’::say_hello’);?//?As?of?5.2.3$myobject?=?new?myclass();call_user_func(array($myobject,?’say_hello’));?>

以上例程會輸出:

Hello!Hello!Hello!

Example #5 Using lambda function with?call_user_func()

<?phpcall_user_func(function($arg)?{?print?'[$arg]n';?},?’test’);?/*?As?of?PHP?5.3.0?*/?>

以上例程會輸出:

[test]call_user_func_array

(PHP 4 >= 4.0.4, PHP 5)

call_user_func_array?— 傳入一個參數數組來調用回調函數

說明mixed?call_user_func_array?(?callable?$callback?,?array?$param_arr?)

Calls the?callback?given by the first parameter with the parameters in?param_arr.

參數callbackThe?callable?to be called.param_arrThe parameters to be passed to the callback, as an indexed array.返回值

Returns the return value of the callback, or?FALSE?on error.

范例

Example #1?call_user_func_array()?example

<?phpfunction?foobar($arg,?$arg2)?{????echo?__FUNCTION__,?'?got?$arg?and?$arg2n';}class?foo?{????function?bar($arg,?$arg2)?{????????echo?__METHOD__,?'?got?$arg?and?$arg2n';????}}//?Call?the?foobar()?function?with?2?argumentscall_user_func_array('foobar',?array('one',?'two'));//?Call?the?$foo->bar()?method?with?2?arguments$foo?=?new?foo;call_user_func_array(array($foo,?'bar'),?array('three',?'four'));?>

以上例程的輸出類似于:

foobar got one and twofoo::bar got three and four

Example #2?call_user_func_array()?using namespace name

<?phpnamespace?Foobar;class?Foo?{????static?public?function?test($name)?{????????print?'Hello?{$name}!n';????}}//?As?of?PHP?5.3.0call_user_func_array(__NAMESPACE__?.’Foo::test’,?array(’Hannes’));//?As?of?PHP?5.3.0call_user_func_array(array(__NAMESPACE__?.’Foo’,?’test’),?array(’Philip’));?>

以上例程的輸出類似于:

Hello Hannes!Hello Philip!

Example #3 Using lambda function

<?php$func?=?function($arg1,?$arg2)?{????return?$arg1?*?$arg2;};var_dump(call_user_func_array($func,?array(2,?4)));?/*?As?of?PHP?5.3.0?*/?>

以上例程會輸出:

int(8)

標簽: PHP
相關文章:
主站蜘蛛池模板: 日韩伦理一区 | 午夜一级影院 | 韩国毛片免费看 | 久久99精品久久久久久 | 草逼免费视频 | 调教套上奶牛榨乳器喷奶水 | 9966久久精品免费看国产 | 达达兔午夜一级毛片 | 自拍偷拍欧美亚洲 | 亚洲成色| 在线观看免费视频网站色 | 热99re久久国超精品首页 | 在线观看免费黄色网址 | 欧美综合精品 | 国产免费高清mv视频在线观看 | 亚洲精品久久成人福利 | 免费黄色一级片 | 视频一区二区三区免费观看 | 沟沟人体一区二区 | 性欧美高清理论片 | 久久色播 | 图片综合区 | 欧美成人综合在线观看视频 | 麻豆果冻传媒一卡二卡 | 国产三级在线视频观看 | 麻豆视频免费 | 精品国产欧美精品v | 1000部18未成人禁止国产 | 国产精品v一区二区三区 | 一级欧美日韩 | 91精品婷婷国产综合久久8 | 亚洲草逼视频 | 国产中文字幕在线 | 日本精品高清一区二区不卡 | 欧美一a一片一级一片 | 免费视频片在线观看 | 干成人网 | 久久综合久久综合久久 | 91网站桃色| 免费一级毛片清高播放 | 亚洲综合久久久 |