我想创建一个目录类,我希望它作为一个迭代器(foreach-able)。但我希望它是基于生成器的(例如一个执行“yield”的函数)。可能吗? 最佳答案 没有为类显式实现生成器的接口(interface),但您可以在此处使用IteratorAggregate。请参阅此从GeneratorsRFC实现IteratorAggregate的示例classTestimplementsIteratorAggregate{protected$data;publicfunction__construct(array$data){$this->da
我正在编写依赖于外部类exceptionManager的单元测试。我希望能够预测此类中的一些特定函数将返回什么,所以我使用了一个模拟对象。代码非常简单:$mockExceptionManager=$this->getMock('exceptionManager');问题是,我的异常管理器实现了IteratorAggregate接口(interface),它需要一个如下所示的方法:publicfunctiongetIterator(){returnnewArrayIterator($this->exceptions);}当我运行单元测试时,出现以下错误:Fatalerror:Cannot
我是php的初学者,是从php.net学习的。该页面(http://php.net/manual/en/class.traversable.php)上的注释说:实现此接口(interface)的内部(内置)类可以在foreach构造中使用,不需要实现IteratorAggregate或Iterator。这个注释说的是什么?这是否意味着我们可以在没有任何类的情况下在foreach循环中使用IteratorAggregate或Iterator或者可能是我错了。谁能说出这个note说的是什么?? 最佳答案 IteratorAggregat
好的,正如标题所说:与简单的foreach循环相比,使用ArrayIterator的主要好处是什么。我有一个用作容器的对象,它的主要职责是存储一个对象数组。有人建议我让我的类实现IteratorAggregate并返回一个ArrayIterator和我的对象数组:像这样:publicfunctiongetIterator(){returnnewArrayIterator($this->_objs);}与简单地返回数组然后使用标准的foreach循环遍历它们相比,我看不出这样做有什么好处。有人能解释一下为什么这是个好主意吗? 最佳答案
IteratorAggregate是一个创建外部迭代器的接口(interface):classmyDataimplementsIteratorAggregate{public$property1="Publicpropertyone";public$property2="Publicpropertytwo";public$property3="Publicpropertythree";publicfunction__construct(){$this->property4="lastproperty";}publicfunctiongetIterator(){returnnewArra
IteratorAggregate是一个创建外部迭代器的接口(interface):classmyDataimplementsIteratorAggregate{public$property1="Publicpropertyone";public$property2="Publicpropertytwo";public$property3="Publicpropertythree";publicfunction__construct(){$this->property4="lastproperty";}publicfunctiongetIterator(){returnnewArra