I proposed a while back adding a new magic method __toArray that would 
automatically cast. I'm halfway through writing the RFC (in draft and 
unpublished). 

I wonder how that could tie in to this proposal. Or even be a precursor to 
this. 

Sent from my iPhone

> On Nov 17, 2019, at 11:10 AM, Ken Stanley <doh...@gmail.com> wrote:
> 
> On Sun, Nov 17, 2019 at 9:42 AM Aimeos | Norbert Sendetzky <
> norb...@aimeos.com> wrote:
> 
>> 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
> 
> 
> Also the toArray() should be called when an Arrayable is cast to an array:
> (array)$arrayable.
> 
> And shouldn’t the toArray match the same naming semantics as __toString?
> -- 
> "Do not go gentle into that good night. Rage, rage against the dying of the
> light." — Dylan Thomas

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

Reply via email to