php - 类型提示模型数组

标签 php arrays laravel type-hinting

我想知道您是否可以在 php(更明确地 Laravel)中键入一组模型。

一个代码示例:

use App\User;

public function index(User $user)
{
    // do something with the user
}

我的问题是,有没有办法对用户模型数组进行类型提示:
use App\User;

public function index(array User $users) //this is wrong ..
{
    // do something with the users
}

最佳答案

不,你不能这样做 。您能做的最好的事情是将参数类型提示为数组。您可以在此处阅读有关 PHP 中类型提示的更多信息:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
更新
在 PHP 5.6+ 中,您可以使用可变长度参数列表来实现您想要的。检查 answer below

关于php - 类型提示模型数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050599/

相关文章:

php - 我们可以将列动态传递到 mysql select 语句中吗

arrays - Ruby append 到数组

c - 用 C 语言编写归并排序伪代码过程

php - Action App\Http\Controllers\Controller@action 未定义

php - 表单未将数据传递到数据库但没有错误 Laravel 5.8.22

php - simplexml_load_string() 将空 xml 元素转换为对象而不是字符串

php - 如何在php作业调度程序功能页面中添加$id

php - PHP 中的多行语句到 MySQL 数据库(采用自动递增 ID)

arrays - vba 数组操作硬编码语法

laravel - 在页脚中使用过滤器进行列搜索 - 页脚过滤器未显示 - Laravel Datatables Yajrabox