Re: Internal iteration API
| From: | Sara Golemon | Date: | Wed, 11 Jul 2012 23:04:59 +0000 |
| Subject: | Re: Internal iteration API | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-61145@lists.php.net to get a copy of this message | ||
Love the idea. +1
On Wed, Jul 11, 2012 at 3:17 PM, Nikita Popov <nikita.ppv@gmail.com> 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
>
>