Re: Supporting ArrayObject in array_* functions

From: Date: Thu, 30 Jul 2009 18:22:21 +0000
Subject: Re: Supporting ArrayObject in array_* functions
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-45153@lists.php.net to get a copy of this message
Hi!
1- A class that implements ArrayAccess must be useable everywhere an "array" is useable;
The problem is that many functions dealing with arrays do not make sense with generic object. For others, it may make sense in theory, but not be practical - e.g., suppose you have ArrayAccess object referring to a database field, how easy would be to make shuffle() work on it? How practical would it be?
2- ArrayAccess needs to be expanded with methods for all the array functions and ArrayObject needs to implement them;
All 70+ of them? That'd be one fat interface.
How would it be achieved? $coll->sort(); instead of sort($coll); $coll2 = $coll->reverse(); instead of $coll2 = array_reverse($coll);
Note that this also eats up a lot of useful names, and makes you implement functions that you would never use, such as picking a random element or finding difference between arrays. I think all array operations should be divided into following classes: 1. Basic ones, probably ArrayAccess/Iterator interface should be enough 2. Compound ones implemented on top of basic ones (i.e. array_sum is a combination of iteration and +, etc.) 3. Additional operations non-reducible to (1), like shift/unsift or exotic ones like array_change_key_case(). First two classes are easy to handle, the last one should be handled separately on per-case basis. -- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com

« previous php.internals (#45153) next »