gpt4 book ai didi

php - 警告 : incorrect argument passed to function

转载 作者:行者123 更新时间:2023-12-01 22:32:21 26 4
gpt4 key购买 nike

我在 ModelTable.php 中有一个函数 beforeMarshal ()。此函数在保存请求数据之前正确执行,但是每次保存数据时我都会收到 php 警告。

public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
{
$ip = $data['ip'];
$data['iplong'] = ip2long($ip);
}

Warning (4096): Argument 1 passed to App\Model\Table\ServersTable::beforeMarshal() must be an instance of App\Model\Table\Event, instance of Cake\Event\Event given

Warning (4096): Argument 2 passed to App\Model\Table\ServersTable::beforeMarshal() must be an instance of App\Model\Table\ArrayObject, instance of ArrayObject given

Warning (4096): Argument 3 passed to App\Model\Table\ServersTable::beforeMarshal() must be an instance of App\Model\Table\ArrayObject, instance of ArrayObject given

如果我将 debug 设置为 false,一切都很好,但这不是一个很好的解决方案。

最佳答案

它可能是一个 namespace问题。错误是该函数需要一个精确类型的对象,但它接收了另一个对象。

我假设 beforeMarshal() 是您自己的自定义函数,它位于命名空间 App\Model\Table 中(因为它属于 ModelTable 类)。

此外,您可能是从其他文件调用它的,您没有在其中指定命名空间,因此它认为自己在全局命名空间中。

要么

  • 在函数定义中键入正确的对象类型提示
  • ModelTable.php 中全局使用正确的对象类型

精确类型提示

将您的函数定义更改为:

public function beforeMarshal(\Cake\Event\Event $event, \ArrayObject $data, \ArrayObject $options)

注意反斜杠。在考虑对象类型时,它们指示函数从全局命名空间开始。

全局使用

在您的 ModelTable.php 脚本(或包含该函数的脚本)的最顶部添加:

use
Cake\Event\Event,
ArrayObject;

因此在整个脚本中,任何对 EventArrayObject 类型的使用都将使用正确的类型,而无需每次都指定完整名称。

选哪个

意见跟随

我更喜欢在我的脚本顶部使用use。它可以防止在这个特定脚本中对类型的任何类型的滥用,当我必须将 ALL\Templating\Parser 更改为 ALL\Parser 时,我只需要一次性完成地方。

每次都指定整个对象命名空间路径很麻烦,而且更容易出错,但可能有好处;自从我开始使用全局方法以来,我没有体验过它们,也没有停止过。

关于php - 警告 : incorrect argument passed to function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com