在 PHP 中,方法参数前面的数组 keword 是什么意思


What does the array keword in front of a method parameter mean in PHP?

我偶然发现了PHP中的函数声明,参数前面有关键字array。现在,由于您不在 PHP 中声明类型,这对我来说真的很有线。这只是一个"错误",某人放在那里的东西没有被评估,还是它实际上意味着什么?

public function sendSMTPMail(array $mailContent) { }

的感觉是它不应该在那里,它什么也没做,但也许我错了?有区别吗

public function sendSMTPMail($mailContent) { }

被称为类型声明,在 PHP5 中也称为类型提示。

类型

声明允许函数将参数指定为某些类型。如果给定的值类型不正确,则会生成错误:在 PHP 5 中,这将是一个可恢复的致命错误,而 PHP 7 将抛出 TypeError 异常。

若要指定类型声明,应在参数名称之前添加类型名称。如果参数的默认值设置为 NULL,则可以使声明接受 NULL 值。

有效类型

Type                    Description                                                Minimum PHP version
----------------------------------------------------------------------------------------------------------
Class/interface name    The parameter must be an instanceof the given class         PHP 5.0.0
                        or interface name.                 
----------------------------------------------------------------------------------------------------------                            
self                    The parameter must be an instanceof the same class as       PHP 5.0.0
                        the one the method is defined on. 
                        This can only be used on class and instance methods.    
----------------------------------------------------------------------------------------------------------
array                   The parameter must be an array.                             PHP 5.1.0
----------------------------------------------------------------------------------------------------------
callable                The parameter must be a valid callable.                     PHP 5.4.0
----------------------------------------------------------------------------------------------------------
bool                    The parameter must be a boolean value.                      PHP 7.0.0
----------------------------------------------------------------------------------------------------------
float                   The parameter must be a floating point number.              PHP 7.0.0
----------------------------------------------------------------------------------------------------------
int                     The parameter must be an integer.                           PHP 7.0.0
----------------------------------------------------------------------------------------------------------
string                  The parameter must be a string.                             PHP 7.0.0
----------------------------------------------------------------------------------------------------------

原始来源:PHP 函数参数类型声明

在您的情况下,请查看以下示例:

function test(array $array)
{
    foreach($array as $k=>$v)
    {
    }
}
test(array("string")); //passed - no error
test("string"); //failed - catchable error

输出:

可捕获的致命错误:传递给 test() 的参数 1 必须是数组类型,给定字符串,在第 12 行的/var/www/html/test/test1.php 中调用,并在第 3 行的/var/www/html/test/test1.php 中定义

它用于类型提示。如果传递给它的数据是其他类型的,那么它将抛出错误(>= PHP 5)。

类型

声明允许函数将参数指定为某些类型。如果给定的值类型不正确,则会生成错误:在 PHP 5 中,这将是一个可恢复的致命错误,而 PHP 7 将抛出 TypeError 异常。

类型声明

如果未定义类型,则变量将被强制转换为传递的数据类型。在这种情况下,如果对传递给它的数据进行任何验证,则可能会在执行过程中引起问题。