Am 04.02.20 um 18:18 schrieb Larry Garfield: >> 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; >> } >> >> 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.
The array internals are outside the scope of this proposal and are not affected in any way by this proposal. > 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. Tried that already, but other core developers made very clear that ArrayObject is one of the worst implementations in PHP and that it's not going to be improved or touched in any way and using it is highly discouraged. > Especially with union types, amalgam built in types like this are > even less needed. If an "arrayable" pseudo type should be implemented is subject to discussion. It would be a short form for "array|Arrayable" (native type or interface for array like objects) only offering a syntactical sugar for developers. > 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. "array|ArrayAccess" is not sufficient because arrays are also countable and traversable and this isn't possible with union data types: interface Arrayable extends ArrayAccess, Countable, Traversable function useArrayable( array|Arrayable $arg ) { $cnt = count( $arg ); $value = $arg['key']; foreach( $arg as $key => $entry ); } -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php