下面来和大家分享一下这个call_user_func_array和call_user_func函数的用法,另外附赠func_get_args()函数和func_num_args()函数,嘿嘿!!
call_user_func函数是当需要动态调用函数时,才使用的,这个函数有两种用法:
第一种是调用孤独的函数:
复制代码 代码如下:
function funa($b,$c)
{
echo $b;
echo $c;
}
call_user_func('funa', "111","222");
call_user_func('funa', "333","444");
//显示 111 222 333 444
//大家有没有发现,这个用法有点像javascript中的call方法,嘿嘿
?>
第二种是调用类内部的函数:
复制代码 代码如下:
class a {
function b()
{
$args = func_get_args();
$num = func_num_args();
print_r($args);
echo $num;
}
}
call_user_func(array("a", "b"),"111","222");
?>
上面这个例子,自己运行一下看看结果是什么吧~嘿嘿~提示一下func_get_args()函数是获取传入到函数中的参数,返回一个数组,func_num_args()函数获取传入函数中的参数的个数。
下面再来看看call_user_func_array函数
这个函数也是当需要动态调用函数时用到的,它的用法和call_user_func函数比较像,,只是参数传入的是数组。
复制代码 代码如下:
function a($b, $c)
{
echo $b;
echo $c;
}
call_user_func_array('a', array("111", "222"));
//显示 111 222
?>
call_user_func_array函数也可以调用类内部的方法的
复制代码 代码如下:
Class ClassA
{
function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array(‘ClassA','bc'), array(“111″, “222″));
//显示 333
?>
下面再看一个动态调用函数的例子:
复制代码 代码如下:
function otest1 ($a)
{
echo( '一个参数' );
}
function otest2 ( $a, $b)
{
echo( '二个参数' );
}
function otest3 ( $a ,$b,$c)
{
echo( '三个啦' );
}
function otest (){
$args = func_get_args();
$num = func_num_args();
call_user_func_array( 'otest'.$num, $args );
}
otest("11");
otest("11","22");
otest("11","22","33");
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools