合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 参数及类型申明 ## **可变参数** 在 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+** ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">sum</span><span class="token3">(</span><span class="token3">.</span><span class="token3">.</span><span class="token3">.</span>$numbers<span class="token3">)</span> <span class="token3">{</span> $acc <span class="token1">=</span> <span class="token6">0</span><span class="token3">;</span> foreach <span class="token3">(</span>$numbers as $n<span class="token3">)</span> <span class="token3">{</span> $acc <span class="token1">+</span><span class="token1">=</span> $n<span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $acc<span class="token3">;</span> <span class="token3">}</span> echo <span class="token4">sum</span><span class="token3">(</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">,</span> <span class="token6">3</span><span class="token3">,</span> <span class="token6">4</span><span class="token3">)</span><span class="token3">;</span><span class="token">//10</span> ``` ``` 您也可以在调用函数以将数组或Traversable变量或文字解压缩到参数列表中时使用...: ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">add</span><span class="token3">(</span>$a<span class="token3">,</span> $b<span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $a <span class="token1">+</span> $b<span class="token3">;</span> <span class="token3">}</span> echo <span class="token4">add</span><span class="token3">(</span><span class="token3">.</span><span class="token3">.</span><span class="token3">.</span><span class="token3">[</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">]</span><span class="token3">)</span><span class="token3">.</span><span class="token2">"\n"</span><span class="token3">;</span><span class="token">//3</span> $a <span class="token1">=</span> <span class="token3">[</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">]</span><span class="token3">;</span> echo <span class="token4">add</span><span class="token3">(</span><span class="token3">.</span><span class="token3">.</span><span class="token3">.</span>$a<span class="token3">)</span><span class="token3">;</span><span class="token">//3</span> ``` ``` 可以将 **...** 方法常规参数的后面 ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">total_intervals</span><span class="token3">(</span>$unit<span class="token3">,</span> DateInterval <span class="token3">.</span><span class="token3">.</span><span class="token3">.</span>$intervals<span class="token3">)</span> <span class="token3">{</span> $time <span class="token1">=</span> <span class="token6">0</span><span class="token3">;</span> foreach <span class="token3">(</span>$intervals as $interval<span class="token3">)</span> <span class="token3">{</span> $time <span class="token1">+</span><span class="token1">=</span> $interval<span class="token1">-</span><span class="token1">></span>$unit<span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $time<span class="token3">;</span> <span class="token3">}</span> $a <span class="token1">=</span> <span class="token5">new</span> <span class="token4">DateInterval</span><span class="token3">(</span><span class="token2">'P1D'</span><span class="token3">)</span><span class="token3">;</span> $b <span class="token1">=</span> <span class="token5">new</span> <span class="token4">DateInterval</span><span class="token3">(</span><span class="token2">'P2D'</span><span class="token3">)</span><span class="token3">;</span> echo <span class="token4">total_intervals</span><span class="token3">(</span><span class="token2">'d'</span><span class="token3">,</span> $a<span class="token3">,</span> $b<span class="token3">)</span><span class="token3">.</span><span class="token2">' days'</span><span class="token3">;</span> <span class="token">//3 days</span> echo <span class="token4">total_intervals</span><span class="token3">(</span><span class="token2">'d'</span><span class="token3">,</span> <span class="token5">null</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// 致命错误, 因为null 不是一个 DateInterval 对象.</span> ``` ``` **PHP 5.6-** ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">sum</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $acc <span class="token1">=</span> <span class="token6">0</span><span class="token3">;</span> foreach <span class="token3">(</span><span class="token4">func_get_args</span><span class="token3">(</span><span class="token3">)</span> as $n<span class="token3">)</span> <span class="token3">{</span> $acc <span class="token1">+</span><span class="token1">=</span> $n<span class="token3">;</span> <span class="token3">}</span> <span class="token5">return</span> $acc<span class="token3">;</span> <span class="token3">}</span> echo <span class="token4">sum</span><span class="token3">(</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">,</span> <span class="token6">3</span><span class="token3">,</span> <span class="token6">4</span><span class="token3">)</span><span class="token3">;</span><span class="token">//10</span> ``` ``` ``` <pre class="calibre10">``` class <span class="token4">Test</span> <span class="token3">{</span> <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $num <span class="token1">=</span><span class="token4">func_num_args</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> switch <span class="token3">(</span>$num<span class="token3">)</span> <span class="token3">{</span> case <span class="token6">0</span><span class="token3">:</span> echo <span class="token2">"none "</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> case <span class="token6">1</span><span class="token3">:</span> echo <span class="token2">"one "</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> case <span class="token6">2</span><span class="token3">:</span> echo <span class="token2">"two "</span><span class="token3">;</span> <span class="token5">break</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token5">new</span> <span class="token4">Test</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//none</span> <span class="token5">new</span> <span class="token4">Test</span><span class="token3">(</span><span class="token6">1</span><span class="token3">)</span><span class="token3">;</span><span class="token">//one</span> <span class="token5">new</span> <span class="token4">Test</span><span class="token3">(</span><span class="token6">1</span><span class="token3">,</span><span class="token6">2</span><span class="token3">)</span><span class="token3">;</span><span class="token">//two</span> ``` ``` ## **类型声明** > 类型声明不支持别名 如`function test(boolean $flag){}`会将boolean解析为boolean类而不是boolean类型 TypeDescriptionMinimum PHP versionClass/interface nameThe 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)类型的值。 ``` <pre class="calibre10">``` <span class="token5">function</span> <span class="token4">test</span><span class="token3">(</span>string $str<span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $str<span class="token3">;</span> <span class="token3">}</span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">test</span><span class="token3">(</span><span class="token6">10</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//string(2) "10"</span> ``` ``` 可以基于每一个文件开启严格模式。在严格模式中,只有一个与类型声明完全相符的变量才会被接受,否则将会抛出一个**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*声明来启用严格模式: > **Caution** > 启用严格模式同时也会影响[返回值类型声明](https://www.php.net/manual/zh/functions.returning-values.php#functions.returning-values.type-declaration). > **Note**: > 严格类型适用于在*启用严格模式的文件内*的函数调用,而不是在那个文件内声明的函数。 一个没有启用严格模式的文件内调用了一个在启用严格模式的文件中定义的函数,那么将会遵循调用者的偏好(弱类型),而这个值将会被转换。 > **Note**: > 严格类型仅用于标量类型声明,也正是因为如此,这需要PHP 7.0.0 或更新版本,因为标量类型声明也是在那个版本中添加的。 **例子** ``` <pre class="calibre10">``` <span class="token1"><</span><span class="token1">?</span>php <span class="token4">declare</span><span class="token3">(</span>strict_types<span class="token1">=</span><span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">sum</span><span class="token3">(</span>int $a<span class="token3">,</span> int $b<span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> $a <span class="token1">+</span> $b<span class="token3">;</span> <span class="token3">}</span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">sum</span><span class="token3">(</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//3</span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">sum</span><span class="token3">(</span><span class="token6">1.5</span><span class="token3">,</span> <span class="token6">2.5</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//致命错误,传递给sum的参数必须是int</span> ``` ``` 例外 申明float时可以传入int类型的参数且自动转为float ``` <pre class="calibre17">``` <span class="token4">declare</span><span class="token3">(</span>strict_types<span class="token1">=</span><span class="token6">1</span><span class="token3">)</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">sum</span><span class="token3">(</span>float $a<span class="token3">,</span> float $b<span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> $a <span class="token1">+</span>$b<span class="token3">;</span> <span class="token3">}</span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">sum</span><span class="token3">(</span><span class="token6">1</span><span class="token3">,</span> <span class="token6">2</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//float(3) </span> <span class="token4">var_dump</span><span class="token3">(</span><span class="token4">sum</span><span class="token3">(</span><span class="token6">1.5</span><span class="token3">,</span><span class="token6">2.5</span><span class="token3">)</span><span class="token3">)</span><span class="token3">;</span><span class="token">//float(4)</span> ``` ```