rfc:foreach-non-scalar-keys

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:foreach-non-scalar-keys [2013/02/19 13:15] nikicrfc:foreach-non-scalar-keys [2013/02/25 14:51] – Resolve iterator_to_array question nikic
Line 66: Line 66:
  
 The signature can use ''zval*'' instead of ''zval**'' because by-ref modification of keys is not possible. The refcount is increased in the handler (instead of the VM) as that turned out to be the more convenient method for this use case. The signature can use ''zval*'' instead of ''zval**'' because by-ref modification of keys is not possible. The refcount is increased in the handler (instead of the VM) as that turned out to be the more convenient method for this use case.
 +
 +===== iterator_to_array() =====
 +
 +When using non-string/int keys ''iterator_to_array'' with the ''$preserve_keys'' option will behave in the same way as PHP would when it does normal array key assignments, i.e. its behavior would be the same as the following PHP snippet:
 +
 +<code php>
 +function iterator_to_array($iter) {
 +    foreach ($iter as $k => $v) {
 +        $array[$k] = $v;
 +    }
 +    return $array;
 +}
 +</code>
 +
 +For array and object keys this would give an ''Illegal offset type'' warning. For ''NULL'' the ''%%""%%'' key is used, doubles are truncated to the integral part, resources use their resource ID and issue a warning, booleans are cast to integers.
 +
 +In order to support this a new function is added in ''Zend/zend_API.h'' (which more or less reimplements the internal inline function ''zend_fetch_dimension_address_inner''):
 +
 +<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);
 +</code>
  
 ===== Patch ===== ===== Patch =====
Line 72: Line 94:
  
 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 ===== 
- 
-The main open question are iterator/array interactions, e.g. when an iterator is converted to an array with ''iterator_to_array''. What should be done with the keys that are valid in the iterator, but not in the array? I think the best approach would be to just set the array keys with the exact same semantics as PHP would do (i.e. with all casts and warnings). I couldn't yet find a function in the API that does this though. Maybe have to expose the inline functions from ''zend_execute.c'' for that :) 
rfc/foreach-non-scalar-keys.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1