ホームページ  >  記事  >  バックエンド開発  >  PHP call_user_func_array

PHP call_user_func_array

王林
王林オリジナル
2024-08-29 12:46:02492ブラウズ

PHP では、call_user_func は、呼び出すための呼び出しを提供する組み込み関数の 1 つであり、PHP で構築される call_user_func_array 呼び出しは、配列にまとめてラップされたパラメーターを渡すためのコールバックとして定義されており、そのようなコールバック関数はcall_user_func_array として知られています。一般に、call_user_func-array は call_user_func として定義されます。これは、最初のパラメータで指定されたコールバックを呼び出すコールバック関数です。これらのパラメータは配列のようにラップされており、PHP ライブラリによって提供される call_user_func_array 関数として知られているため、この関数はユーザーを呼び出します。指定されたパラメータの配列を持つ関数。

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

mixed call_user_func_array( callable $callback, array $args);

上記の構文では、呼び出し可能なパラメーターは呼び出す必要がある「呼び出し可能」なコールバック関数であり、2 番目のパラメーター「args」は、この「args」パラメーターがパラメーターに渡されるパラメーターの配列です。コールバック関数をインデックス付き配列として使用します。したがって、呼び出し可能関数は呼び出し可能関数として知られるカスタム関数を呼び出すことができ、他のパラメーターはパラメーター配列で構成されます。PHP プログラムでこの関数を使用する場合は、この call_user_func-array の両方の引数を指定することが必須です。したがって、この組み込み関数は、最初の引数で指定または宣言されたコールバックを呼び出し、残りのパラメーターは、通常ユーザー定義関数を呼び出すために使用されるこの関数の 2 番目の引数のパラメーターの配列として渡されます。
上記の構文関数は、条件が満たされた場合にコールバックの戻り値を返します。それ以外の場合、エラーが発生した場合は false を返します。

call_user_func_array() 関数はどのように機能しますか?

Php には、組み込み関数 call_func_user() があり、通常、コールバック関数を呼び出すために使用されます。この関数は、通常、呼び出し可能なパラメーターが呼び出されるように宣言されているこの関数の最初のパラメーターとして渡されます。これと同じ関数は、残りのパラメータを引数として配列として渡すために使用されます。PHP には call_user_func_array() などの別の関数があり、この関数は次のように機能します。最初にパラメータをチェックします。最初のパラメータは常にコールバック関数になります。 callable であり、この関数に呼び出された後、残りのパラメータをパラメータの配列として、呼び出されるコールバックの 2 番目の引数として渡します。詳細には、最初のパラメーターのコールバックは、この call_user_func_array 関数を指定する前にプログラムで定義されている関数を呼び出します。その後、この関数はその名前を使用して call 関数内で宣言され、関数のパラメーターを 2 番目の引数として渡します。残りのパラメータとして 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:PHP array_pop()次の記事:PHP array_pop()