在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 的兩個參數。因此,這個內建函數呼叫在第一個參數中指定或宣告的回調,其餘參數則作為參數陣列傳遞到該函數的第二個參數中,該函數通常用於呼叫使用者定義的函數。
如果滿足條件為真,上面的語法函數將傳回回呼的回傳值,否則如果導致任何錯誤,則傳回假。
在Php中,有一個內建函數call_func_user(),通常用於呼叫回呼函數,回呼函數將作為第一個參數傳遞給該函數,通常聲明可呼叫參數將被呼叫。同樣的函數用於將剩餘參數作為參數作為數組傳遞,PHP 中還有另一個函數,例如call_user_func_array() ,該函數的工作原理如下,它將首先檢查參數,其中第一個參數始終是回調函數callable並被調用,然後我們將剩餘的參數作為參數數組傳遞給被調用的回調函數的第二個參數。詳細來說,我們可以說第一個參數回調將呼叫在指定此call_user_func_array 函數之前在程式中定義的函數,然後使用其名稱在呼叫函數中聲明該函數,我們將將該函數的參數作為第二個參數傳遞給call_user_func_array 作為剩餘參數,它將是一個參數數組。讓我們透過以下部分的範例來清楚地了解該函數的工作原理。
代碼:
<?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")); ?>
輸出:
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.
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:
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.
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中文網其他相關文章!