RE: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface

From: Date: Mon, 18 Nov 2019 08:54:03 +0000
Subject: RE: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable interface
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-107818@lists.php.net to get a copy of this message
The union types RFC passed, so I didn't see much value in the arrayable pseudo except pressing less keys. Regarding the Arrayable interface, or toArray method. Since it extends Iterator, iterator_to_array can do the job already. So what's the benfits for introducing this new interface? I would suggest to find a way to make those built-in array_* functions support the arraylike objects, no matter which interface it requires, existing ArrayAccess, or any new interface. Regards, CHU Zhaowei > -----Original Message----- > From: Aimeos | Norbert Sendetzky <norbert@aimeos.com> > Sent: Sunday, November 17, 2019 10:41 PM > To: internals@lists.php.net > Subject: [PHP-DEV] Concept: "arrayable" pseudo type and \Arrayable > interface > > Since PHP 7.1 there's the "iterable" pseudo type hint that matches > "array" > and "Traversable". > > PHP frameworks would profit from support of 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 { > $cnt = 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 native arrays: > > if( $arrayable instanceof \Arrayable ) { > $arrayable = $arrayable->toArray(); > } > > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: > http://www.php.net/unsub.php >

« previous php.internals (#107818) next »