致命错误:在中的非对象上调用成员函数 create()
对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
替换
为
这当然只能确保在未设置网关的情况下不会调用代码,因此它不会对 $item 执行任何操作(这可能不是您想要的)。
Replace
with
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).
已经得到了!
看起来,如果请求的集合没有任何对象,它只是尝试做一些事情。
如果我首先计算项目并将其与 > 进行比较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.
这仅意味着
$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
this will fix this error, but can lead to more errors further down, depending on what exactly is
$item
supposed to be.