zephir
zephir copied to clipboard
ArrayIterator and `for` loop
I have simple Arr helper with getIterator() method:
class Arr implements \ArrayAccess, \Countable, \IteratorAggregate
{
protected _data = [] { get };
public function getIterator()
{
return new \ArrayIterator(this->_data);
}
//...
Is it possible to ArrayIterator work with for loop?
let tokens = new Arr([1,2,3]);
for token in tokens {
var_dump(token);
}
The argument is not initialized or iterable()
So I must use:
for token in tokens->getData() {
var_dump(token);
}
This way:
for token in iterator(tokens) {
var_dump(token);
}
Remember that for questions like this you can better use the forum: http://forum.zephir-lang.com/
Thanks, I didn't know about the forum.
Could Zephir auto-detect if object have getIterator() method? What do you think?
You mean at compile time or runtime?
At compile time, before error check if argument has iterator.