rfc:foreach-non-scalar-keys
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | rfc:foreach-non-scalar-keys [2013/02/19 12:47] – update nikic | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Allow Non-Scalar Keys ====== | ||
+ | * version 1.0 | ||
+ | * Date: 2013-01-28 | ||
+ | * Authors: Levi Morrison < | ||
+ | * Status: Under Discussion | ||
+ | ===== Current situation ===== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code php> | ||
+ | $it = new MultipleIterator; | ||
+ | $it-> | ||
+ | $it-> | ||
+ | |||
+ | // This is NOT possible | ||
+ | foreach ($it as $keys => $values) { | ||
+ | // ... | ||
+ | } | ||
+ | |||
+ | // Instead you have to use this | ||
+ | foreach ($it as $values) { | ||
+ | $keys = $it-> | ||
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code php> | ||
+ | // NOT possible | ||
+ | foreach ($objectStore as $key => $value) { | ||
+ | // ... | ||
+ | } | ||
+ | |||
+ | // Instead you have to use | ||
+ | foreach ($objectStore as $key) { | ||
+ | $value = $objectStore[$key]; | ||
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | These are just two examples from core classes, but it obviously also applies in many other cases (and now that we have generators, it will probably become an even larger issue). | ||
+ | |||
+ | Another key issue is that you can't really work around this generically. If you want to write code that is also compatible with '' | ||
+ | |||
+ | ===== Suggested fix ===== | ||
+ | |||
+ | This RFC proposes to lift the restriction and allow values of arbitrary types to be used as keys (in particularly allowing also arrays and objects) in iterators. (Note: This proposal does not suggest allowing those key types in arrays. This is only about '' | ||
+ | |||
+ | In order to remove this restriction the internal [['' | ||
+ | |||
+ | <code c> | ||
+ | // This entry: | ||
+ | int (*get_current_key)(zend_object_iterator *iter, char **str_key, uint *str_key_len, | ||
+ | // Is replaced with this entry: | ||
+ | zval *(*get_current_key)(zend_object_iterator *iter TSRMLS_DC); | ||
+ | </ | ||
+ | |||
+ | The handler will return a '' | ||
+ | |||
+ | The signature can use '' | ||
+ | |||
+ | ===== Patch ===== | ||
+ | |||
+ | A preliminary patch implementing the above proposal can be found here: https:// | ||
+ | |||
+ | The change itself is rather small, but there are quite a few extensions that require minor adjustments to use the new API. | ||
+ | |||
+ | ===== Open questions ===== | ||
+ | |||
+ | The main open question are iterator/ |
rfc/foreach-non-scalar-keys.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1