`array`关键字在PHP函数的参数列表中意味着什么?

Ben*_*son -3 php arrays parameters function

function request($suffix, array $post = array(), $action = NULL) {
    // do something...
}
Run Code Online (Sandbox Code Playgroud)

我在书中看到了一个功能.但我不明白该关键字在函数的参数列表中的array含义$post.我觉得省略array遗嘱会产生同样的效果.

它是否充当数据类型提示符?如果是这样,那么为什么我不能用string,integer等先于其他参数?

Cez*_*ary 5

function request($suffix, array $post = array(), $action = NULL) {
    // do something...
}
Run Code Online (Sandbox Code Playgroud)

该函数接受多个参数,参数$ post是类型暗示为数组

array $post = array()
Run Code Online (Sandbox Code Playgroud)

在这里我们可以看到$ post被默认分配一个空数组,因此它符合它的类型

$post = array() and array $post = array() will return the same
Run Code Online (Sandbox Code Playgroud)

如果你尝试传递除了数组之外的其他东西作为该函数的参数,你将得到错误

类型提示是指更多解释

  • 请注意,在PHP5中,此类型提示将被解释为要求第一个参数是类"字符串"的实例而不是字符串.因此在PHP5中,将字符串传递给以这种方式编写的类会导致错误.在PHP7中,需要一个字符串. - 来源:https://torquemag.io/2016/09/wordpress-developers-guide-type-hinting/ @Benson (2认同)
  • http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration (2认同)