Skip to content

Add support for non-scalar foreach keys - #278

Closed
nikic wants to merge 3 commits into
php:masterfrom
nikic:allowNonScalarIteratorKeys
Closed

nikic wants to merge 3 commits into
php:masterfrom
nikic:allowNonScalarIteratorKeys

Conversation

@nikic

@nikic nikic commented Feb 18, 2013

Copy link
Copy Markdown
Member

This is just a preliminary patch for the https://wiki.php.net/rfc/foreach-non-scalar-keys RFC.

@nikic

nikic commented Feb 18, 2013

Copy link
Copy Markdown
Member Author

This patch basically changes the signature for get_current_key from

int (*get_current_key)(zend_object_iterator *iter, char **str_key, uint *str_key_len, ulong *int_key TSRMLS_DC);

to

zval *(*get_current_key)(zend_object_iterator *iter TSRMLS_DC);

and does the necessary changes in all extensions (hopefully I didn't miss any ^^).

The get_current_key handler returns a zval* with already increased refcount. The return value may be NULL if and only if an exception was thrown. (Maybe this exception should be removed too, because right now I'm not sure this case is handled properly everywhere.)

Things still to do: Figure out what to do with non-string/int keys when converting to array (iterator_to_array / CacheIterator).

This commit adds a new ZEND_API function array_set_zval_key, that sets
the key in the zval with the same semantics and errors as PHP would
normally do. This new function is used to implement iterator_to_array
and CachingIterator for non-scalar keys. In particular
iterator_to_array() should now behave exactly the same as doing a
manual loop with array insertion:

    foreach ($it as $k => $v) {
	    $array[$k] = $v;
    }
@datibbaw

Copy link
Copy Markdown
Contributor

Awesome work! 👍

@wpajqz

wpajqz commented Feb 28, 2013

Copy link
Copy Markdown

good

发自我的小米手机

datibbaw notifications@github.com编写:

Awesome work! 👍


Reply to this email directly or view it on GitHub:
#278 (comment)

@weltling

weltling commented Mar 1, 2013

Copy link
Copy Markdown
Contributor

Which tests can/should I run to test the patch?

@nikic

nikic commented Mar 2, 2013

Copy link
Copy Markdown
Member Author

@weltling Zend/tests and ext/spl/tests should cover the main thing. Though this change is mostly changing the use of the API in extensions all over the place, so "all of them" might be better :)

@weltling

weltling commented Mar 2, 2013

Copy link
Copy Markdown
Contributor

@nikic Ok, thought that :) I was just wondering there are no tests for the special case mentioned in the RFC. Performed them altogether now, looks good.

Comment thread Zend/zend_hash.c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently, zend_hash.c didn't depend on zval struct, will it better move the function to zend_API ?

@nikic

nikic commented Mar 12, 2013

Copy link
Copy Markdown
Member Author

Merged in fcc6611.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants