【发布时间】:2010-08-17 07:35:18
【问题描述】:
对于一个 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 是集合使用的适配器。我不使用它并将其保持为空。或许与此有关?
有人知道吗?因为一切都在正常运行,所以我可以读取 collectiondata。这只是错误。
【问题讨论】:
-
调用这个方法的时候
$this->_gateway初始化了吗?
标签: php collections