rfc:foreach-non-scalar-keys
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:foreach-non-scalar-keys [2013/02/19 12:47] – update nikic | rfc:foreach-non-scalar-keys [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Allow Non-Scalar Keys ====== | + | ====== Allow non-scalar keys in '' |
* version 1.0 | * version 1.0 | ||
* Date: 2013-01-28 | * Date: 2013-01-28 | ||
* Authors: Levi Morrison < | * Authors: Levi Morrison < | ||
- | * Status: | + | * Status: |
===== Current situation ===== | ===== Current situation ===== | ||
Line 47: | Line 47: | ||
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). | 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 '' | + | 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 ===== | ===== Suggested fix ===== | ||
Line 53: | Line 53: | ||
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 '' | 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 [['' | + | In order to remove this restriction the internal [[http:// |
<code c> | <code c> | ||
Line 59: | Line 59: | ||
int (*get_current_key)(zend_object_iterator *iter, char **str_key, uint *str_key_len, | int (*get_current_key)(zend_object_iterator *iter, char **str_key, uint *str_key_len, | ||
// Is replaced with this entry: | // Is replaced with this entry: | ||
- | zval *(*get_current_key)(zend_object_iterator *iter TSRMLS_DC); | + | void (*get_current_key)(zend_object_iterator *iter, zval *key TSRMLS_DC); |
</ | </ | ||
- | The handler | + | The handler |
- | The signature can use '' | + | ===== iterator_to_array() ===== |
+ | |||
+ | When using non-string/ | ||
+ | |||
+ | <code php> | ||
+ | function iterator_to_array($iter) { | ||
+ | foreach ($iter | ||
+ | $array[$k] = $v; | ||
+ | } | ||
+ | return $array; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | For array and object keys this would give an '' | ||
+ | |||
+ | In order to support this a new function is added in '' | ||
+ | |||
+ | <code c> | ||
+ | /* The refcount of value is incremented by the function itself */ | ||
+ | ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value); | ||
+ | </ | ||
===== Patch ===== | ===== Patch ===== | ||
- | A preliminary | + | The patch for this change |
The change itself is rather small, but there are quite a few extensions that require minor adjustments to use the new API. | The change itself is rather small, but there are quite a few extensions that require minor adjustments to use the new API. | ||
- | ===== Open questions | + | ===== Vote ===== |
+ | |||
+ | Voting ends on March 6th. A 50% + 1 majority is required. This RFC targets PHP 5.5. | ||
- | The main open question are iterator/ | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </ |
rfc/foreach-non-scalar-keys.1361278021.txt.gz · Last modified: 2017/09/22 13:28 (external edit)