Extend ArrayObject class

php.internals

Aimeos | Norbert Sendetzky

6 years ago
The ArrayObject class is only rarely used at the moment due to a lack for almost all array_* functions beside the sort functions. Currently implemented by ArrayObject: https://www.php.net/manual/en/class.arrayobject.php Missing methods that would be most useful: array_​chunk array_​column array_​diff_​assoc array_​diff_​key array_​diff_​uassoc array_​diff_​ukey array_​diff array_​filter array_​flip array_​intersect_​assoc array_​intersect_​key array_​intersect_​uassoc array_​intersect_​ukey array_​intersect array_​key_​first array_​key_​last array_​keys array_​map array_​merge array_​pop array_​push array_​rand array_​reduce array_​replace_​recursive array_​replace array_​reverse array_​search array_​shift array_​slice array_​splice array_​unique array_​unshift array_​values shuffle Furthermore, the already existing sort functions return VOID, which isn't best for an object. Instead, returning the ArrayObject itself to support a fluid interface would be much better, i.e.: $ao = new \ArrayObject([null, 'a' => 2, 'b' => 1]); $ao->filter()->sort()->flip(); This change wouldn't break any existing code already using the ArrayObject. It should be possible to extend from ArrayObject too and have access to the internal array. Therefore, a new method would be required: protected getArrayRef() : array The method is similar to the existing getArrayCopy() method but returns a reference to the internal array instead of a copy. Thus, classes extending ArrayObject will be able to implement additional methods efficiently without the need to create copies of the internal array first.

Marco Pivetta

6 years ago
On Sun, Nov 24, 2019, 13:16 Aimeos | Norbert Sendetzky <norbert@aimeos.com> wrote:
> The ArrayObject class is only rarely used at the moment due to a lack > for almost all array_* functions beside the sort functions. > > Currently implemented by ArrayObject: > https://www.php.net/manual/en/class.arrayobject.php > > Missing methods that would be most useful: > array_​chunk > array_​column > array_​diff_​assoc > array_​diff_​key > array_​diff_​uassoc > array_​diff_​ukey > array_​diff > array_​filter > array_​flip > array_​intersect_​assoc > array_​intersect_​key > array_​intersect_​uassoc > array_​intersect_​ukey > array_​intersect > array_​key_​first > array_​key_​last > array_​keys > array_​map > array_​merge > array_​pop > array_​push > array_​rand > array_​reduce > array_​replace_​recursive > array_​replace > array_​reverse > array_​search > array_​shift > array_​slice > array_​splice > array_​unique > array_​unshift > array_​values > shuffle > > > Furthermore, the already existing sort functions return VOID, which > isn't best for an object. Instead, returning the ArrayObject itself to > support a fluid interface would be much better, i.e.: > > $ao = new \ArrayObject([null, 'a' => 2, 'b' => 1]); > $ao->filter()->sort()->flip(); > > This change wouldn't break any existing code already using the ArrayObject. > > > It should be possible to extend from ArrayObject too and have access to > the internal array. Therefore, a new method would be required: > > protected getArrayRef() : array > > The method is similar to the existing getArrayCopy() method but returns > a reference to the internal array instead of a copy. Thus, classes > extending ArrayObject will be able to implement additional methods > efficiently without the need to create copies of the internal array first. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.ph > <http://www.php.net/unsub.php>
Ouff, please don't extend `ArrayObject`: it is very quirky and keeps state in a way that is very much untransparent to userland: please use `ArrayAccess` instead. Even then, the various `array_` functions that mutate/map/filter would need to produce equivalent data structures by knowing the ctor of your array-alike object: better leave them alone, unless the output is a core `array` again