不能在命名参数后使用位置参数 |Yii2 [英] Cannot use positional argument after named argument | Yii2

查看:29
本文介绍了不能在命名参数后使用位置参数 |Yii2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Yii2 PHP 框架课程,但我遇到了一个问题.我和PHP不太相似,所以我很难解决这个问题.

所以,我正在尝试使删除"按钮,我收到错误消息:不能在命名参数后使用位置参数".

有代码片段:

'class' =>'yii\grid\ActionColumn','按钮' =>['删除' =>函数($url){返回 Html::a(text:'Delete', $url, ['数据方法' =>'邮政']);}]

解决方案

这不是 Yii2 问题,如果您检查 php 文档页面 它谈到了这个案例,它说:

<块引用>

命名参数可以与位置参数结合使用.在这种情况下,命名参数必须在位置参数之后.也可以只指定函数的一些可选参数,而不管它们的顺序.

这是有道理的,如果 PHP 让您先放置命名参数,然后放置位置,就不可能确定哪些参数应该与您实际传递的 未命名 参数匹配.

在相反的情况下,第一个位置参数根据它们的顺序与函数参数一一匹配,然后,命名参数允许您在对默认值感到满意但想要为它们后面的其他参数赋值.

最简单的解决方案是,就像 Barmar 在评论中所说,删除 text: 并使用位置参数,您的参数已经在正确的顺序中,并且您正在使用所有参数.

您也可以命名所有参数.

顺便说一句,我认为在这种情况下的错误消息是不言自明且非常清楚的.

I'm in the Yii2 PHP Framework course, and I have one problem. I'm not similar with the PHP very well, so it's hard to me to solve that problem.

So, I'm trying to make the "Delete" button in my web, and I'm receiving error: "Cannot use positional argument after named argument".

There is the fragment of code:

'class' => 'yii\grid\ActionColumn',
'buttons' => [
    'delete' => function ($url) {
        return Html::a(text:'Delete', $url, [
            'data-method' => 'post'
        ]);
    }
]

解决方案

This is not a Yii2 problem, this behavior is expected and documented, if you check the php documentation pages it talks about this case, it says:

Named arguments can be combined with positional arguments. In this case, the named arguments must come after the positional arguments. It is also possible to specify only some of the optional arguments of a function, regardless of their order.

Which makes sense, if PHP let you place named arguments first, then positional, it would be impossible to determine what parameters should be matched with the unnamed arguments you are actually passing.

On the opposite situation, first positional arguments get matched one to one with the function parameters based on their order, then, named arguments allow you to skip having to give values to parameters when you are happy with their default value but want to assign a value to some other parameter that follows them.

The easiest solution is, like Barmar said in the comments, to remove text: and use positional arguments, your arguments are already in the correct order, and you are using all the parameters.

You could also make all your arguments named.

As a side note, I think the error message in this case is self-explanatory and very clear.

这篇关于不能在命名参数后使用位置参数 |Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆