致命错误:在中的非对象上调用成员函数 create()

发布于 2024-09-14 19:34:38 字数 1025 浏览 2 评论 0原文

对于 php 项目,我使用 Collection 类来处理我的对象并在类似 Java 的集合中进行延迟加载。

例如,现在我的对象有一个电子邮件地址的集合。因此,我调用对象的 getEmailAddresses() 函数,该函数调用映射器来返回电子邮件地址的集合。

这工作正常,但是当我对集合执行 foreach 循环时,它返回有效数据,最后出现以下错误:

Fatal error: Call to a member function create() on a non-object in /foo/bar/Collection.php on line 89

它指向以下函数:

public function current()
    {
        if ($this->_collection instanceof Iterator)
            $key = $this->_collection->key();
        else
            $key = key($this->_collection);
        if ($key === null)
            return false;
        $item = $this->_collection[$key];
        if (!is_object($item)) {
            $item = $this->_gateway->create($item);
            $this->_collection[$key] = $item;
        }
        return $item;
    }

此行:

$item = $this->_gateway->create($item);

_gateway 是集合使用的适配器。我不使用它并将其保留为空。也许与此有关?

有人有一些线索吗?因为一切都正常运行,所以我可以读取集合数据。这只是错误。

For a php project I use a Collection class to handle my objects and lazyloading in Java-like collections.

Now my object has a collection of emailaddresses for example. So I call the getEmailAddresses() function of the object which calls the mapper to return a collection of emailaddresses.

This works fine, but when I do a foreach loop over my collection it returns valid data with the following error in the end:

Fatal error: Call to a member function create() on a non-object in /foo/bar/Collection.php on line 89

It directs to the following function:

public function current()
    {
        if ($this->_collection instanceof Iterator)
            $key = $this->_collection->key();
        else
            $key = key($this->_collection);
        if ($key === null)
            return false;
        $item = $this->_collection[$key];
        if (!is_object($item)) {
            $item = $this->_gateway->create($item);
            $this->_collection[$key] = $item;
        }
        return $item;
    }

This line:

$item = $this->_gateway->create($item);

The _gateway is the adapter the collection uses. Which I don't use and keep it null. Maybe it has something to do with that?

Anybody has some clue? Because everything is functioning as it should, I can read the collectiondata. It's just the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温折酒 2024-09-21 19:34:38

替换

if (!is_object($item))

if (!is_object($item) && !is_null($this->_gateway))

这当然只能确保在未设置网关的情况下不会调用代码,因此它不会对 $item 执行任何操作(这可能不是您想要的)。

Replace

if (!is_object($item))

with

if (!is_object($item) && !is_null($this->_gateway))

This of course only makes sure the code doesn't get called if gateway isn't set, so it doesn't do anything to $item (which might not be what you want).

东风软 2024-09-21 19:34:38

已经得到了!

看起来,如果请求的集合没有任何对象,它只是尝试做一些事情。

如果我首先计算项目并将其与 > 进行比较0 它不会返回任何错误。这将是一个问题,因此我将更新该类以首先检查它。

我不是唯一一个使用它的人,这不是您所期望的错误。

Already got it!

It appears that if the collection requested doesnt have any objects, it just tries to do something.

If I first count the items and compare it to > 0 it doesnt return any errors. This is going to be an issue so I'll update the class to check for it first.

I'm not the only one working with it, this is not an error you would expect.

逆夏时光 2024-09-21 19:34:38

这仅意味着 $this->_gateway 不是一个对象,而它应该是一个对象。它不能为空。

您可以更改此行:

$item = $this->_gateway->create($item);

到此

if(is_object($this->_gateway)) {
  $item = $this->_gateway->create($item);
}

将修复此错误,但可能会导致更多错误,具体取决于什么正是 $item 应该是的。

This only means that $this->_gateway is not an object and it should be. It can't be null.

You can change this line:

$item = $this->_gateway->create($item);

to

if(is_object($this->_gateway)) {
  $item = $this->_gateway->create($item);
}

this will fix this error, but can lead to more errors further down, depending on what exactly is $item supposed to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文