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

时间:2010-08-17 07:35:19

标签: php collections

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

现在我的对象有一组emailaddresses例如。所以我调用调用mapper的对象的getEmailAddresses()函数来返回一个emailaddresses的集合。

这样可以正常工作,但是当我对我的集合执行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是集合使用的适配器。我不使用并保持null。也许它与此有关?

有人有一些线索吗?因为一切都按预期运行,我可以阅读收集数据。这只是错误。

3 个答案:

答案 0 :(得分:0)

替换

if (!is_object($item))

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

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

答案 1 :(得分:0)

已经搞定了!

如果请求的集合没有任何对象,它似乎只是尝试做某事。

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

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

答案 2 :(得分:-1)

这仅表示$this->_gateway不是对象,应该是。它不能为空。

您可以更改此行:

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

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

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