PHP call_user_func_array

王林
发布: 2024-08-29 12:46:02
原创
584 人浏览过

在PHP中,call_user_func是内置函数之一,它提供对invoke的调用,在PHP中构造的是call_user_func_array调用,它被定义为用于传递完全包装在数组中的参数的回调,这样的回调函数是称为 call_user_func_array。一般来说,call_user_func-array 被定义为 call_user_func,它是一个回调函数,它调用第一个参数给出的回调,这些参数包装在数组中,称为 call_user_func_array 函数,由 PHP 库提供,因此该函数调用用户具有给定参数数组的函数。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法:

mixed call_user_func_array( callable $callback, array $args);
登录后复制

在上面的语法中,callable参数是一个需要调用的“callable”回调函数,第二个参数“args”是一个参数数组,这个“args”参数要传递给作为索引数组的回调函数。因此,可调用函数可以调用称为可调用函数的自定义函数,另一个参数由参数数组组成,其中在 PHP 程序中使用此函数时,必须指定此 call_user_func-array 的两个参数。因此,这个内置函数调用在第一个参数中指定或声明的回调,其余参数作为参数数组传递到该函数的第二个参数中,该函数通常用于调用用户定义的函数。
如果满足条件为真,上面的语法函数将返回回调的返回值,否则如果导致任何错误,则返回假。

call_user_func_array() 函数如何工作?

在Php中,有一个内置函数call_func_user(),通常用于调用回调函数,回调函数将作为第一个参数传递给该函数,通常声明可调用参数将被调用。同样的函数用于将剩余参数作为参数作为数组传递,PHP 中还有另一个函数,例如 call_user_func_array() ,该函数的工作原理如下,它将首先检查参数,其中第一个参数始终是回调函数callable 并被调用,然后我们将剩余的参数作为参数数组传递给被调用的回调函数的第二个参数。详细来说,我们可以说第一个参数回调将调用在指定此 call_user_func_array 函数之前在程序中定义的函数,然后使用其名称在调用函数中声明该函数,我们将将该函数的参数作为第二个参数传递给call_user_func_array 作为剩余参数,它将是一个参数数组。让我们通过以下部分的示例来清楚地了解该函数的工作原理。

示例#1

代码:

<?php
echo "Demonstration of call_user_func_array in PHP ";
echo " \n";
function func1($a, $b) {
echo __FUNCTION__, " got $a and $b\n";
}
class class_func {
function bar($a, $b) {
echo __METHOD__, " got $a and $b\n";
}
}
call_user_func_array("func1", array("one", "two"));
$class_func = new class_func;
call_user_func_array(array($class_func, "bar"), array("three", "four"));
?>
登录后复制

输出:

PHP call_user_func_array

In the above example, we can see that to write any PHP program we first start with “ ”. Then to print any message to the console then we use the “echo” command. Firstly we declare a function with the name “func1” and this function has two different parameters passed to this function func1 and this function has only an echo command which will print the values passed as a parameter to this function when called. This function func1() is called using a callback parameter in the call_user_func_array as seen in line 16 and then we pass values to the parameters declared for the function func1() as “one” and “two” which will be taken as an array of parameters to the call-user_func_array where this is one of the helpful facts of this function where we can call the function along with the values of its parameters passed using any number of parameters to the given or defined function in the program. Then we are using the class method also where we are defining a class with the name “class_func” and then using the class object created using the “new” command, later we use the call_user_func_array function to print the values of the function “bar” which is defined within the class, therefore, we first pass the class name as the first parameter to the array function and then “bar” function as the second parameter to the array function then later we will be passing remaining parameters of the bar as the second argument to the array function. The output of the above program can be seen in the screenshot above.

Example #2

In this example, we will see calling the call_user_func_array using the namespace name in the below program. In the above program, we saw using the class method for the demonstration of call_user_func_array.

Code:

<?php
namespace func1;
echo "Demonstration of call_user_func_array in PHP using namespace ";
echo " \n ";
class class_func {
static public function test($n) {
print "Hello {$n}!\n";
}
}
call_user_func_array(__NAMESPACE__ .'\class_func::test', array('Educba'));
call_user_func_array(array(__NAMESPACE__ .'\class_func', 'test'), array('People'));
?>
登录后复制

Output:

PHP call_user_func_array

In the above program which is similar to the previous example but in this program we are using namespace as function name and then we are declaring the class where when using call_user_func_array we use “_NAmESPACE_” instead of “_FUNCTION_” in it and then pass the other parameters as an array to this function which will print the values or logic of the class “test” function where it prints the values passed as the “n” value along with the message given and in this program, we are printing two messages by passing these “values”. The output of this program is as shown in the above screenshot.

Conclusion

In this article, we conclude that in PHP call_user_func_array function as a callback function which is user-defined as we are declaring the callable callback function as the first argument and the remaining parameter of that function as the second argument. We should note that we are passing parameters with its values but we are not passing the reference. Therefore, this function can be used whenever there are any array of parameters for the function we can easily use and execute the given function using this call_user_func_array function in PHP.

以上是PHP call_user_func_array的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!