rfc:foreach-non-scalar-keys
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:foreach-non-scalar-keys [2013/02/01 23:36] – [Introduction] Expanded introduction. levim | rfc:foreach-non-scalar-keys [2013/03/12 16:47] – link commit nikic | ||
---|---|---|---|
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: | ||
+ | zval *(*get_current_key)(zend_object_iterator *iter TSRMLS_DC); | ||
+ | </ | ||
- | } | + | The handler will return a '' |
- | $requestA = new StdClass; | + | The signature can use '' |
- | $requestA-> | + | |
- | $requestA-> | + | |
- | $responseA | + | ===== iterator_to_array() ===== |
- | $responseA-> | + | |
- | $responseA-> | + | |
- | $requestB = new StdClass; | + | When using non-string/int keys '' |
- | $requestB-> | + | |
- | $requestB-> | + | |
- | $responseB = new StdClass; | + | <code php> |
- | $responseB-> | + | function iterator_to_array($iter) { |
- | $responseB-> | + | |
+ | $array[$k] = $v; | ||
+ | } | ||
+ | return $array; | ||
+ | } | ||
+ | </ | ||
- | $requests = [$requestA, $requestB]; | + | For array and object keys this would give an '' |
- | $responses = [$responseA, $responseB]; | + | |
- | $mapIterator = new MapIterator( | + | In order to support this a new function is added in '' |
- | $requests, | + | |
- | $responses | + | |
- | ); | + | |
- | foreach ($mapIterator as $request => $response) { | + | <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); |
- | } | + | |
</ | </ | ||
- | ===== 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 warning will also be removed. This also opens the possibility to use arrays as keys returned from an iterator, and as such I feel we should also add support for `list` in the keys. | + | A preliminary patch implementing |
- | 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.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1