Req #78801 [NEW]: Add "arrayable" pseudo type hint to PHP

From: Date: Sat, 09 Nov 2019 14:48:00 +0000
Subject: Req #78801 [NEW]: Add "arrayable" pseudo type hint to PHP
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223661@lists.php.net to get a copy of this message
From: norbert at linuxnetworks dot de Operating system: PHP version: Irrelevant Package: *General Issues Bug Type: Feature/Change Request Bug description:Add "arrayable" pseudo type hint to PHP Description: ------------ Since PHP 7.1 there's the "iterable" pseudo type hint that matches "array" and "Traversable". It would be great if PHP would support an "arrayable" pseudo type hint that matches "array" and all objects that implements "Traversable", "ArrayAccess" and "Countable". Thus, we could pass arrays or all objects that behave like arrays to methods and do: function useArrayable( arrayable $arg ) : arrayable { count( $arg ); $value = $arg['key']; foreach( $arg as $key => $entry ) { ... } return $arg; } It would be useful to implement an Arrayable interface too: interface Arrayable extends \Iterator, \Countable, \ArrayAccess { public function toArray() : array; } Then, we can use array like objects: class Test implements \Arrayable { // ... } $arrayable = new Test(); $arrayable['key'] = $value; $arrayable = useArrayable( $arrayable ); And if necessary, we can convert them to arrays: if( $arrayable instanceof \Arrayable ) { $arrayable = $arrayable->toArray(); } -- Edit bug report at https://bugs.php.net/bug.php?id=78801&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=78801&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=78801&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=78801&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=78801&r=needscript Try newer version: https://bugs.php.net/fix.php?id=78801&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=78801&r=support Expected behavior: https://bugs.php.net/fix.php?id=78801&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=78801&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=78801&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=78801&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=78801&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=78801&r=dst IIS Stability: https://bugs.php.net/fix.php?id=78801&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=78801&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=78801&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=78801&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=78801&r=mysqlcfg

« previous php.bugs (#223661) next »