搜索
首页后端开发php教程PHP call_user_func_array

PHP call_user_func_array

Aug 29, 2024 pm 12:46 PM
php

在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中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP与Python:用例和应用程序PHP与Python:用例和应用程序Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具