Req #78801 [Opn->Sus]: Add "arrayable" pseudo type hint to PHP

From: Date: Mon, 11 Nov 2019 12:30:55 +0000
Subject: Req #78801 [Opn->Sus]: Add "arrayable" pseudo type hint to PHP
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223678@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78801&edit=1 ID: 78801 Updated by: cmb@php.net Reported by: norbert at linuxnetworks dot de Summary: Add "arrayable" pseudo type hint to PHP -Status: Open +Status: Suspended Type: Feature/Change Request Package: *General Issues PHP Version: Irrelevant Block user comment: N Private report: N New Comment: This feature should be discussed on the internals mailing list[1] (and might require an RFC). Please forward your request to there. For the time being, I'm suspending this ticket. [1] <https://www.php.net/mailing-lists.php#internals> Previous Comments: ------------------------------------------------------------------------ [2019-11-09 14:48:00] norbert at linuxnetworks dot de 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 this bug report at https://bugs.php.net/bug.php?id=78801&edit=1

« previous php.bugs (#223678) next »