PHP call_user_func_array

王林
Release: 2024-08-29 12:46:02
Original
490 people have browsed it

In PHP, the call_user_func is one of the inbuilt in function which provides calls to invoke which is constructed in PHP is call_user_func_array call which is defined as a callback for passing the parameters which are altogether wrapped in an array and such callback function is known as call_user_func_array. In general, call_user_func-array is defined as a call_user_func which is a callback function where it calls the callback given by the first parameter with these parameter wrapped as in array is known as call_user_func_array function which is provided by PHP libraries therefore this function calls a user function with the given array of parameters.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

mixed call_user_func_array( callable $callback, array $args);
Copy after login

In the above syntax, the callable parameter is a callback function that is “callable” that needs to be called, and the second parameter “args” is an array of parameters where this “args” parameter is to be passed to the callback function as an indexed array. Therefore, the callable can call a custom function known as callable function and the other parameter consists of a parameter array where both arguments of this call_user_func-array are mandatory to specify when using this function in PHP programs. Hence this inbuilt function calls the callback which is specified or declared in the first argument and the remaining parameters are passed as an array of parameters in the second argument of this function which is usually used to call the user-defined functions.
The above syntax function will return the return value of the callback if satisfied the condition is true else it will return false if it results in any error.

How call_user_func_array() function works?

In Php, there is an inbuilt function call_func_user() which is usually used for calling the callback function which will be passed as the first parameter to this function where the callable parameter is usually declared to be called. This same function is used for passing remaining parameters as arguments as array there is another function in PHP such as call_user_func_array() and this function works as follows, it will first check for the parameters where the first parameter will always be the callback function which is callable and is called then to this function we are passing the remaining parameters as an array of the parameter as the second argument to the callback which is called. In detail, we can say the first parameter callback will call the function which is defined in the program before specifying this call_user_func_array function then this function is declared within the call function using its name and we will be passing the function’s parameter as a second argument to the call_user_func_array as remaining parameters which will be an array of parameters. Let us the clear working of this function with examples in the below section.

Example #1

Code:

<?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"));
?>
Copy after login

Output:

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'));
?>
Copy after login

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.

The above is the detailed content of PHP call_user_func_array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!