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();
}
On Sun, Nov 17, 2019 at 9:42 AM Aimeos | Norbert Sendetzky <
norbert@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();
}--
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 frameworks would profit from support of an "arrayable" pseudo type
hint that matches "array" and all objects that implements "Traversable",
"ArrayAccess" and "Countable".
...
It would be useful to implement an Arrayable interface too:
interface Arrayable extends \Iterator, \Countable, \ArrayAccess
{
public function toArray() : array;
}
It feels like there are two alternative suggestions here. Not only do
they use the same keyword to mean different things, but "convertible to
array", and "usable as array" seem like distinct concepts to me.
For the "convertible to array" case, I think __toArray, or an interface
specifying just that one method, would make more sense than combining it
with the existing interfaces. I'm sceptical of that concept, though,
because most objects could be converted to many different arrays in
different circumstances, each of which should be given a different and
descriptive name.
For the "usable as array" case, the big advantage would come if internal
array_* functions could take such objects, but that requires a bit of
thought on how each function should behave. There could also be some
surprising behaviour passing infinite iterators, or objects where the
output of ArrayAccess and iteration don't match up. It might make sense
to have objects opt into an Arrayable interface, to make clear they're
expecting to be used this way; but require no additional methods other
than those required by Iterator (or Traversable), Countable, and
ArrayAccess.
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
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
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
interfaceSince 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();
}--
To unsubscribe, visit:
http://www.php.net/unsub.php