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/01/29 05:54] – [Proposal and Patch] 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 |
- | Currently if you have a class that implements [[http:// | + | The '' |
+ | |||
+ | The '' | ||
<code php> | <code php> | ||
- | <?php | + | $it = new MultipleIterator; |
+ | $it-> | ||
+ | $it-> | ||
- | class MapIterator implements Iterator { | + | // This is NOT possible |
- | | + | foreach ($it as $keys => $values) { |
- | protected | + | |
- | | + | } |
- | function __construct(array $keys, array $values) { | + | // Instead you have to use this |
- | $this->keys = $keys; | + | foreach |
- | $this->vals = $values; | + | $keys = $it->keys(); |
- | } | + | |
+ | // ... | ||
+ | } | ||
+ | </ | ||
- | function rewind() { | + | '' |
- | $this-> | + | |
- | } | + | |
- | function valid() { | + | <code php> |
- | return | + | // NOT possible |
- | } | + | foreach |
+ | | ||
+ | } | ||
- | function key() { | + | // Instead you have to use |
- | | + | foreach |
- | } | + | $value = $objectStore[$key]; |
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
- | function current() { | + | 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). |
- | return $this-> | + | |
- | } | + | |
- | function next() { | + | 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 ===== |
- | $requestA = new StdClass; | + | 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 '' |
- | $requestA-> | + | |
- | $requestA-> | + | |
- | $responseA = new StdClass; | + | In order to remove this restriction the internal [[http://lxr.php.net/ |
- | $responseA-> | + | |
- | $responseA-> | + | |
- | $requestB = new StdClass; | + | <code c> |
- | $requestB-> | + | // This entry: |
- | $requestB->headers = [' | + | 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); | ||
+ | </code> | ||
- | $responseB = new StdClass; | + | The handler will return a '' |
- | $responseB-> | + | |
- | $responseB-> | + | |
- | $requests = [$requestA, $requestB]; | + | The signature can use '' |
- | $responses = [$responseA, | + | |
- | $mapIterator | + | ===== iterator_to_array() ===== |
- | $requests, | + | |
- | $responses | + | |
- | ); | + | |
- | foreach ($mapIterator | + | When using non-string/ |
- | | + | |
- | | + | <code php> |
+ | function iterator_to_array($iter) { | ||
+ | | ||
+ | $array[$k] = $v; | ||
+ | | ||
+ | return | ||
} | } | ||
</ | </ | ||
- | Yields a warning | + | For array and object keys this would give an '' |
- | and '' | + | |
+ | 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 | ||
+ | </ | ||
- | ===== 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