Php 简明教程
PHP - Passing Functions
在 PHP 中,除了标量类型、数组和对象以外,您还可以将函数作为其参数之一传递给一个函数。如果定义一个函数来接受另一个函数作为参数,那么传递的函数将在该函数内部调用。PHP 的标准库具有某些此类型的内置函数,其中要传递的参数之一是一个函数,它可能是另一个内置函数,甚至是一个用户定义的函数。
To a function in PHP, in addition to scalar types, arrays, and objects, you can also pass a function as one of its arguments. If a function is defined to accept another function as an argument, the passed function will be invoked inside it. PHP’s standard library has certain built-in functions of this type, where one of the arguments to be passed is a function, which may be another built-in function or even a user defined function.
array_map
array_map() 是其中一个内置函数。此函数的第一个参数是一个回调函数。其他的参数可能是一个或多个数组。回调函数应用于数组的所有元素。
The array_map() is one of the built-in functions. The first argument to this function is a callback function. There may be one or more arrays as the other arguments. The callback function is applied to all the elements of arrays.
array_map(?callable $callback, array $array, array ...$arrays): array
array_map() 函数返回一个数组。它包含将回调函数应用于传递为其他参数的数组的相应元素后得到的结果。
The array_map() function returns an array. It contains the result of applying the callback function to the corresponding elements of arrays passed as other arguments.
Example
在以下示例中,我们有一个 square() 函数,用于计算传递给它的数字的平方。该函数又作为 array_map() 函数的参数使用,以及另一个数字数组。每个数字都先后传递给 square() 函数。结果数组是平方的列表。
In the following example, we have a square() function that computes the square of a number passed to it. This function in turn is used as an argument for array_map() function, along with another array of numbers. Each number is successively passed to the squares() function. The resultant array is a list of squares.
<?php
function square($number) {
return $number * $number;
}
$arr = [1, 2, 3, 4, 5];
$squares = array_map('square', $arr);
var_dump($squares);
?>
它将生成以下 output −
It will produce the following output −
array(5) {
[0]=>
int(1)
[1]=>
int(4)
[2]=>
int(9)
[3]=>
int(16)
[4]=>
int(25)
}
call_user_func
将函数传递给另一个函数的另一示例是 call_user_func()。顾名思义,它调用另一个用户定义的回调函数,并将其他参数传递到回调。
Another example of passing a function to another function is call_user_func(). As the name suggests, it calls another user defined callback function, and the other arguments are passed to the callback.
call_user_func(callable $callback, mixed ...$args): mixed
Example
在下面的示例中,square() 函数被反复调用,传递数组中的每个数字。
In the example below, the square() function is invoked repeatedly, passing each number in an array.
<?php
function square($number) {
return $number * $number;
}
$arr = [1, 2, 3, 4, 5];
foreach($arr as $a) {
echo "square of $a:" . call_user_func("square", $a). PHP_EOL;
}
?>
它将生成以下 output −
It will produce the following output −
square of 1:1
square of 2:4
square of 3:9
square of 4:16
square of 5:25
usort
作为函数传递的另一个示例,我们来看一下 usort() 函数。
As another example of passing function, we take a look a usort() function.
usort(array &$array, callable $callback): true
第一个参数是一个数组。数组将按照作为第二个参数的回调函数进行排序。
The first parameter is an array. The array is sorted as per the callback function, which is the second parameter.
回调参数是一个比较函数,如果第一个参数被认为分别小于、等于或大于第二个参数,则该函数必须返回一个小于零、等于零或大于零的整数。
The callback parameter is a comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Example
这里有一个示例。我们首先有一个 mysort() 函数。如果第一个数字小于、等于或大于第二个数字,它将比较两个数字并返回“-1”、“0”或“1”。
Here is an example. First we have a mysort() function. It compares two numbers and returns "-1", "0" or "1" if the first number is less than, equal to or greater than second number.
usort() 的第一个参数是 mysort() 函数,第二个参数是一个数组。首先,前两个数字将传递给 mysort()。如果它返回 1,那么它们将被交换。接下来,第二个和第三个数字被传递,如果比较返回 1,则它们将被交换。相同的过程重复进行,以便按升序排列数组元素。
The first argument to usort() is the mysort() function, and the second one is an array. To begin with, the first two numbers are passed to mysort(). If it returns 1, they are swapped. Next, the second and third numbers are passed and swapped if the comparison returns 1. The same process repeats so that the array elements are arranged in ascending order.
<?php
function mysort($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "mysort");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?>
它将生成以下 output −
It will produce the following output −
0: 1
1: 2
2: 3
3: 5
4: 6
Pass Callback to User-defined Function
除了上述的内置函数外,你还可以定义你自己接受其中一个参数作为另一个函数的函数。
Apart from the above built-in functions, you can define your own function that accepts one of the arguments as another function.
在下面的示例中,我们有两个函数, square() 和 cube() ,它们返回给定数字的平方和平方根。
In the example below, we have two functions, square() and cube(), that return the square and cube of a given number.
接下来,是 myfunction() ,它的第一个参数用作一个可变函数,第二个参数传递给 myfunction() 。
Next, there is myfunction(), whose first argument is used as a variable function and the second argument to myfunction() is passed to it.
因此,myfunction() 在内部调用 square() 或 cube() 来返回给定数字的平方或立方。
Thus, myfunction() internally calls square() or cube() to return either square or cube of a given number.
Example
<?php
function myfunction($function, $number) {
$result = $function($number);
return $result;
}
function cube($number) {
return $number ** 2;
}
function square($number) {
return $number ** 3;
}
$x = 5;
$cube = myfunction('cube', $x);
$square = myfunction('square', $x);
echo "Square of $x = $square" . PHP_EOL;
echo "Cube of $x = $cube" . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
Square of 5 = 125
Cube of 5 = 25