Re: Internal iteration API
Love the idea. +1
On Wed, Jul 11, 2012 at 3:17 PM, Nikita Popov <[email protected]> wrote:
> Hi internals!
>
> Currently PHP does not have an internal iteration API that supports
> both arrays and Traversable objects. Because of that it is currently
> not really possible to write functions (or language features) that
> work on arbitrary traversables. That's probably also the reason why
> function like array_sum() currently work only on arrays and arrays
> only.
>
> So I'd really like to have such an API. One idea for an implementation
> would be this:
> * Create a zend_create_iterator_from_zval() function, which returns a
> zend_object_iterator
> * For arrays and plain objects this would create a thin wrapper
> around the zend_hash_* iteration functions
> * For objects defining get_iterator this would just return the
> object_iterator that get_iterator returned
> * Usage:
>
> zend_object_iterator *iter = zend_create_iterator_from_zval(zval);
> if (iter->rewind) iter->rewind(iter);
> while (iter->valid(iter)) {
> zval **value;
> iter->get_current_data(iter, &value);
> // do something
> iter->move_forward(iter);
> }
> iter->dtor(iter);
>
> I like this approach because it reuses the existing
> zend_object_iterator API. But I see that this is rather an abuse than
> a use :)
>
> Thoughts?
>
> Nikita
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Thread (24 messages)