Re: [PHP-DEV] [RFC] "arrayable" pseudo type hint

This is only part of a thread. view whole thread
  107853
November 24, 2019 12:53 norbert@aimeos.com (Aimeos | Norbert Sendetzky)
> 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.