Re: [RFC] "arrayable" pseudo type hint
| From: | Aimeos | Norbert Sendetzky | Date: | Sun, 24 Nov 2019 12:53:02 +0000 |
| Subject: | Re: [RFC] "arrayable" pseudo type hint | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107853@lists.php.net to get a copy of this message | ||
> Since PHP 7.1, there's the "iterable" pseudo type hint that matches
> "array" or "Traversable".
>
> PHP frameworks would profit from support of an "arrayable" pseudo type
> hint that matches "array" and all objects that can be used like arrays.
>
> For that, "arrayable" objects have to implement these interfaces:
> - ArrayAccess
> - Countable
> - Traversable (i.e. either Iterator or IteratorAggregate)
>
>
> Implementing "arrayable" pseudo type, we could pass arrays or all
> objects that can be used like arrays to methods and do:
>
> function useArrayable( arrayable $arg ) : arrayable {
> $cnt = count( $arg );
> $value = $arg['key'];
> foreach( $arg as $key => $entry );
> return $arg;
> }
Implementation is pretty straight forward:
https://github.com/aimeos/php-src/commit/8c4f7f1142e20696b9481f4329928a6140eb05fd
It also contains is_arrayable() and ReflectionClass::isArrayable()
methods. Tests haven't been implemented yet.