【发布时间】:2015-05-26 17:51:43
【问题描述】:
我有一个班级 Foo,我需要做:
$foo = new Foo();
foreach($foo as $value)
{
echo $value;
}
并定义我自己的方法来迭代这个对象,例如:
class Foo
{
private $bar = [1, 2, 3];
private $baz = [4, 5, 6];
function create_iterator()
{
//callback to the first creation of iterator for this object
$this->do_something_one_time();
}
function iterate()
{
//callback for each iteration in foreach
return $this->bar + $this->baz;
}
}
我们可以这样做吗?怎么样?
【问题讨论】: