> On Nov 17, 2019, at 9:41 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".

If we are going to open up arrays for enhancement in PHP 8 I would ask that we 
seriously consider addressing the various differences between a built-in array 
and an instance of ArrayObject and/or the related associated interfaces such 
that the objects can be used interchangeably with a built-in array, i.e. that 
ArrayObject and the related associated interfaces can fully replace the use of 
arrays when refactoring.

Specifically I would propose that we provide some type of mechanism that would 
allow the developer to cause is_array() to return true on an 
ArrayObject/interfaces.  Maybe something like this (I have not considered all 
the special cases, I am just spitballing in concept here):

class MyArray extends ArrayObject {
   function __AsArray() {
      return true;
   }
   function hello() {
      echo 'Hi!';
   }
}   
echo is_array(new MyArray())  // outputs: true
   ? 'true' 
   : 'false';  

Also, when the object is used by any of the internal PHP functions that expect 
an array, such as sort() the internal function would treat the object as an 
array inside the function.  This also means returning the object from methods 
like array_filter() when an ArrayObject with __AsArray()===true is passed.

Even though these objects would be treated as arrays when in an array context 
they should still be able to call their own methods, e.g.

$array = new MyArray()
$array->hello()  // outputs: Hi!
   ? 'true' 
   : 'false';  

This would go a long way to allowing PHP developers to incrementally clean up 
the gobs of legacy code in the wild, and move to objects where appropriate.

-Mike

Reply via email to