On Tue, Feb 4, 2020, at 6:48 AM, Aimeos | Norbert Sendetzky wrote:
> I would like to modify my initial concept of an "arrayable" type because
> PHP core developers seems to be in favor of the upcoming union data
> types instead of adding a new "arrayable" pseudo type similar to "iterable".
> 
> So, I would like to propose an "Arrayable" interface that combines
> ArrayAccess, Countable and Traversable interfaces and adds a toArray()
> method:
> 
> interface Arrayable extends ArrayAccess, Countable, Traversable
> {
>     public function toArray() : array;
> }
> 
> Then, methods signatures can support array and Array-like objects:
> 
> function useArrayable( array|Arrayable $arg ) : array|Arrayable {
>     $cnt = count( $arg );
>     $value = $arg['key'];
>     foreach( $arg as $key => $entry );
>     is_object( $arg ) { $nativeArray = $arg->toArray(); }
>     return $arg;
> }
> 
> Adding a toArray() method also avoids controversy around using a magic
> __toArray() method, even if this would be easier for developers in this
> case.
> 
> If union data types are available and "iterable" is implemented as
> alias, an alias "arrayable" for "array|Arrayable" may be added as well.

The more I think on it, the less I like `arrayable`.  PHP arrays are a terrible 
data structure from a type system point of view, with too much functionality 
crammed into one variable type.

If you want an object that shoves all the various bits of arrayness into one 
object, there's already ArrayObject, which you can extend if desired.

Especially with union types, amalgam built in types like this are even less 
needed.

Aside from iterable, the only other built-in alias I would see is one for 
array|ArrayAccess.  But... it's now possible to do exactly like that, so I 
don't know how useful it would be.

--Larry Garfield

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to