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/01 23:36] – [Introduction] Expanded introduction. levim | 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 | ||
- | * Author: Levi Morrison < | + | * Authors: Levi Morrison <levim@php.net>, |
- | * Status: | + | * Status: |
- | ===== Introduction | + | ===== Current situation |
- | Implementing the [[http:// | + | The '' |
+ | |||
+ | The '' | ||
<code php> | <code php> | ||
- | <?php | + | $it = new MultipleIterator; |
+ | $it-> | ||
+ | $it-> | ||
- | // $key cannot be an object or array | + | // This is NOT possible |
- | foreach($object | + | foreach ($it as $keys => $values) { |
+ | // ... | ||
+ | } | ||
+ | // Instead you have to use this | ||
+ | foreach ($it as $values) { | ||
+ | $keys = $it-> | ||
+ | | ||
+ | // ... | ||
} | } | ||
</ | </ | ||
- | If you implement | + | '' |
- | ===== Example Iterator ===== | ||
<code php> | <code php> | ||
- | <?php | + | // NOT possible |
+ | foreach ($objectStore as $key => $value) { | ||
+ | // ... | ||
+ | } | ||
- | class MapIterator implements Iterator | + | // Instead you have to use |
- | | + | foreach ($objectStore as $key) { |
- | | + | $value = $objectStore[$key]; |
- | | + | |
+ | | ||
+ | } | ||
+ | </ | ||
- | function __construct(array $keys, array $values) { | + | 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). |
- | $this-> | + | |
- | $this-> | + | |
- | } | + | |
- | function rewind() { | + | Another key issue is that you can't really work around this generically. If you want to write code that is also compatible with '' |
- | | + | |
- | | + | |
- | function valid() { | + | ===== Suggested fix ===== |
- | return $this-> | + | |
- | } | + | |
- | function key() { | + | 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 '' |
- | return $this-> | + | |
- | } | + | |
- | function current() { | + | In order to remove |
- | return $this->vals[$this-> | + | |
- | } | + | |
- | function next() { | + | <code c> |
- | $this-> | + | // This entry: |
- | } | + | int (*get_current_key)(zend_object_iterator *iter, char **str_key, uint *str_key_len, |
+ | // Is replaced with this entry: | ||
+ | void (*get_current_key)(zend_object_iterator *iter, zval *key TSRMLS_DC); | ||
+ | </ | ||
- | } | + | The handler has to write into the passed '' |
- | $requestA | + | ===== iterator_to_array() ===== |
- | $requestA-> | + | |
- | $requestA-> | + | |
- | $responseA = new StdClass; | + | When using non-string/int keys '' |
- | $responseA-> | + | |
- | $responseA-> | + | |
- | $requestB = new StdClass; | + | <code php> |
- | $requestB-> | + | function iterator_to_array($iter) { |
- | $requestB-> | + | |
+ | $array[$k] = $v; | ||
+ | } | ||
+ | return $array; | ||
+ | } | ||
+ | </ | ||
- | $responseB = new StdClass; | + | For array and object keys this would give an '' |
- | $responseB-> | + | |
- | $responseB-> | + | |
- | $requests = [$requestA, $requestB]; | + | In order to support this a new function is added in '' |
- | $responses = [$responseA, | + | |
- | $mapIterator = new MapIterator( | + | <code c> |
- | $requests, | + | /* The refcount of value is incremented by the function itself */ |
- | $responses | + | ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value); |
- | ); | + | |
- | + | ||
- | foreach ($mapIterator as $request => $response) { | + | |
- | | + | |
- | | + | |
- | } | + | |
</ | </ | ||
- | ===== Proposal and Patch ===== | + | ===== Patch ===== |
- | I propose that we lift the restriction that forces a scalar value. Instead we simply assign the key variable to whatever was returned from the iterator. | + | The patch for this change can be found here: https:// |
- | There is no patch at this time. I know Ekneuss was working on something | + | The change itself |
- | ===== Changelog | + | ===== Vote ===== |
- | version | + | Voting ends on March 6th. A 50% + 1 majority is required. This RFC targets PHP 5.5. |
- | * proposed | + | |
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ |
rfc/foreach-non-scalar-keys.1359761791.txt.gz · Last modified: 2017/09/22 13:28 (external edit)