Allow Non-Scalar Keys

Introduction

Implementing the Iterator interface allows a class to define the necessary things to use that class as an iterator. The biggest use-case for implementing the Iterator interface is that it interacts with a foreach loop in a custom way. There are currently limitations on what you can use as the key part of the iterator:

<?php// $key cannot be an object or arrayforeach($objectas$key=>$value){}

If you implement the iterator interface and return an object or array for the key, you get this error: Warning: Illegal type returned from MapIterator::key() in …

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.