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/01 23:36] – [Introduction] Expanded introduction. levim | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Allow Non-Scalar Keys ====== | ||
+ | * version 1.0 | ||
+ | * Date: 2013-01-28 | ||
+ | * Author: Levi Morrison < | ||
+ | * Status: Under Discussion | ||
+ | |||
+ | ===== Introduction ===== | ||
+ | |||
+ | Implementing the [[http:// | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | // $key cannot be an object or array | ||
+ | foreach($object as $key => $value) { | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | If you implement the iterator interface and return an object or array for the key, you get this error: '' | ||
+ | |||
+ | ===== Example Iterator ===== | ||
+ | <code php> | ||
+ | <?php | ||
+ | |||
+ | class MapIterator implements Iterator { | ||
+ | protected $vals = []; | ||
+ | protected $keys = []; | ||
+ | protected $index = 0; | ||
+ | |||
+ | function __construct(array $keys, array $values) { | ||
+ | $this-> | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | function rewind() { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | function valid() { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | function key() { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | function current() { | ||
+ | return $this-> | ||
+ | } | ||
+ | |||
+ | function next() { | ||
+ | $this-> | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | $requestA = new StdClass; | ||
+ | $requestA-> | ||
+ | $requestA-> | ||
+ | |||
+ | $responseA = new StdClass; | ||
+ | $responseA-> | ||
+ | $responseA-> | ||
+ | |||
+ | $requestB = new StdClass; | ||
+ | $requestB-> | ||
+ | $requestB-> | ||
+ | |||
+ | $responseB = new StdClass; | ||
+ | $responseB-> | ||
+ | $responseB-> | ||
+ | |||
+ | $requests = [$requestA, $requestB]; | ||
+ | $responses = [$responseA, | ||
+ | |||
+ | $mapIterator = new MapIterator( | ||
+ | $requests, | ||
+ | $responses | ||
+ | ); | ||
+ | |||
+ | foreach ($mapIterator as $request => $response) { | ||
+ | var_dump($request); | ||
+ | var_dump($response); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Proposal and 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. | ||
+ | |||
+ | There is no patch at this time. I know Ekneuss was working on something but hasn't had time to finish. I also don't know how closely his patch matches up with this proposal, either. | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | version 1.0: | ||
+ | * proposed | ||
rfc/foreach-non-scalar-keys.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1