🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## **可变参数** 在 PHP 5.6 及以上的版本中,由 **...** 语法实现;在 PHP 5.5 及更早版本中,使用函数[func\_num\_args()](https://www.php.net/manual/zh/function.func-num-args.php),[func\_get\_arg()](https://www.php.net/manual/zh/function.func-get-arg.php),和[func\_get\_args()](https://www.php.net/manual/zh/function.func-get-args.php) **PHP 5.6+** ``` function sum(...$numbers) {     $acc = 0;     foreach ($numbers as $n) {         $acc += $n;     }     return $acc; } echo sum(1, 2, 3, 4);//10 ``` 您也可以在调用函数以将数组或Traversable变量或文字解压缩到参数列表中时使用...: ``` function add($a, $b) {     return $a + $b; } echo add(...[1, 2])."\n";//3 $a = [1, 2]; echo add(...$a);//3 ``` 可以将 **...** 方法常规参数的后面 ``` function total_intervals($unit, DateInterval ...$intervals) { $time = 0; foreach ($intervals as $interval) { $time += $interval->$unit; } return $time; } $a = new DateInterval('P1D'); $b = new DateInterval('P2D'); echo total_intervals('d', $a, $b).' days'; //3 days echo total_intervals('d', null); // 致命错误, 因为null 不是一个 DateInterval 对象. ``` **PHP 5.6-** ``` function sum() { $acc = 0; foreach (func_get_args() as $n) { $acc += $n; } return $acc; } echo sum(1, 2, 3, 4);//10 ``` ``` class Test { function __construct() { $num =func_num_args(); switch ($num) { case 0: echo "none "; break; case 1: echo "one "; break; case 2: echo "two "; break; } } } new Test();//none new Test(1);//one new Test(1,2);//two ``` ## **类型声明** >[danger] 类型声明不支持别名 如`function test(boolean $flag){}`会将boolean解析为boolean类而不是boolean类型 | Type | Description | Minimum PHP version | | --- | --- | --- | | Class/interface name | The parameter must be an[*instanceof*](https://www.php.net/manual/zh/language.operators.type.php)the given class or interface name. | PHP 5.0.0 | | *self* | The parameter must be an[*instanceof*](https://www.php.net/manual/zh/language.operators.type.php)the same class as the one the method is defined on. This can only be used on class and instance methods. | PHP 5.0.0 | | [array](https://www.php.net/manual/zh/language.types.array.php) | The parameter must be an[array](https://www.php.net/manual/zh/language.types.array.php). | PHP 5.1.0 | | [callable](https://www.php.net/manual/zh/language.types.callable.php) | 声明参数必须是一个有效的回调函数[callable](https://www.php.net/manual/zh/language.types.callable.php). | PHP 5.4.0 | | [bool](https://www.php.net/manual/zh/language.types.boolean.php) | 声明参数必须是[boolean](https://www.php.net/manual/zh/language.types.boolean.php). | PHP 7.0.0 | | [float](https://www.php.net/manual/zh/language.types.float.php) | 声明参数必须是[float](https://www.php.net/manual/zh/language.types.float.php)或者 number. | PHP 7.0.0 | | [int](https://www.php.net/manual/zh/language.types.integer.php) | 声明参数必须是[integer](https://www.php.net/manual/zh/language.types.integer.php). | PHP 7.0.0 | | [string](https://www.php.net/manual/zh/language.types.string.php) | 声明参数必须是[string](https://www.php.net/manual/zh/language.types.string.php). | PHP 7.0.0 | ## **严格类型** 默认情况下,如果能做到的话,PHP将会强迫错误类型的值转为函数期望的标量类型。 例如,一个函数的一个参数期望是[string](https://www.php.net/manual/zh/language.types.string.php),但传入的是[integer](https://www.php.net/manual/zh/language.types.integer.php),最终函数得到的将会是一个[string](https://www.php.net/manual/zh/language.types.string.php)类型的值。 ``` function test(string $str){ return $str; } var_dump(test(10));//string(2) "10" ``` 可以基于每一个文件开启严格模式。在严格模式中,只有一个与类型声明完全相符的变量才会被接受,否则将会抛出一个**TypeError**。 唯一的一个例外是可以将[integer](https://www.php.net/manual/zh/language.types.integer.php)传给一个期望[float](https://www.php.net/manual/zh/language.types.float.php)的函数。 使用[*declare*](https://www.php.net/manual/zh/control-structures.declare.php)语句和*strict\_types*声明来启用严格模式: >[danger] **Caution** 启用严格模式同时也会影响[返回值类型声明](https://www.php.net/manual/zh/functions.returning-values.php#functions.returning-values.type-declaration). > **Note**: > 严格类型适用于在*启用严格模式的文件内*的函数调用,而不是在那个文件内声明的函数。 一个没有启用严格模式的文件内调用了一个在启用严格模式的文件中定义的函数,那么将会遵循调用者的偏好(弱类型),而这个值将会被转换。 > **Note**: > 严格类型仅用于标量类型声明,也正是因为如此,这需要PHP 7.0.0 或更新版本,因为标量类型声明也是在那个版本中添加的。 **例子** ``` <?php declare(strict_types=1); function sum(int $a, int $b) {     return $a + $b; } var_dump(sum(1, 2));//3 var_dump(sum(1.5, 2.5));//致命错误,传递给sum的参数必须是int ``` 例外 申明float时可以传入int类型的参数且自动转为float ``` declare(strict_types=1); function sum(float $a, float $b){ return $a +$b; } var_dump(sum(1, 2));//float(3) var_dump(sum(1.5,2.5));//float(4) ```