ID: 44734
User updated by: Kai dot Kunstmann at combase dot de
Reported By: Kai dot Kunstmann at combase dot de
Status: Bogus
Bug Type: Feature/Change Request
Operating System: any
PHP Version: 5.2.5
New Comment:
I know that this is no bug -- I didn't report this as a bug but rather
as a feature request. Personally, I know how to use Objects, but other
people may not, and those people will fail to replace arrays with
ArrayObjects.
I like PHP's intense OOP approach, but I don't like the new - yet
familiar looking - "ArrayObject" syntax. Its both confusing and
dangerous to use, as you might be tempted to simple replace "array()"
with "new ArrayObject()", which will not work. People won't see the
advantage of using objects, if using objects will crash their scripts.
For now, ArrayObject and its come-along syntax - that looks exactly the
same as for plain arrays - is just a nice gimmick noone will be pleased
to use. It's just "yet another PHP magic". Well, it's worse than this as
there is no "obvious" advantage to it. You even have to pay a price for
using it by implementing all those utility classes and methods yourself
-- otherwise it simply wouldn't work. And that's exactly the point I
wanted to make, and what this report was for: to request improvement on
the integration of ArrayObject and its siblings with the existing
interface, to reduce the costs for using it.
A short symbolism: ArrayObject is to PHP what anonymous class
instantiation is to Java -- it's there but scarcely anybody uses it, as
long as redundant overhead outbalances its advantage.
Previous Comments:
------------------------------------------------------------------------
[2008-05-25 11:41:02] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
ArrayObject/ArrayIterator implement ArrayAccess and do exactly what
they are supposed to do. They add a new semantics to objects. This is
for instance used in SimpleXML. If you do not like it don\'t use it.
Otherwise read the documentation. Aside from that I cannot find any
substantial information in this report.
------------------------------------------------------------------------
[2008-04-15 16:40:10] Kai dot Kunstmann at combase dot de
the code used for the sample output is actually this:
ArrayObject = new ArrayObject();
$ArrayObject[] = 'foo';
$ArrayObject[] = 'bar';
$ArrayObject[] = 479;
sort($ArrayObject);
var_dump('sorted', $ArrayObject);
$values = array_values($ArrayObject);
var_dump('values', $values);
var_dump('is_array', is_array($ArrayObject));
------------------------------------------------------------------------
[2008-04-15 15:57:52] Kai dot Kunstmann at combase dot de
Description:
------------
As you probably know, objects extending or implementing certain classes
and interfaces from the SPL allow for special array syntax.
e.g. the array-push idiom on the ArrayAccess interface:
<? $ArrayAccessImpl[] = $element; ?>
...or the foreach-loop on Iterator:
<? foreach($ArrayIteratorImpl as $element) { ... } ?>
...ArrayObject even allows for the list-each idiom:
<? while(list($k,$v) = each($ArrayObject)) { ... } ?>
This is a nice feature, which I very much appreciate. However, it is
error prone as it is not complete. The avarage PHP user, none-OOP
programmer and not-about-type-caring guy will certainly fail to replace
arrays with ArrayObject and that alike, as it simply is not the same.
It's just the same syntax for two different semantics.
I like the idea to be able to implement my own Collection classes and
iterate over instances just like in Java. Taking that road however
requires me to implement my own utility methods like 'sort()' and
derivates (yes, some build-ins fail to sort an $ArrayObject) and
replacements for all those high-performance array_* methods.
Replacing those is necessary because the only common thing ArrayObject
and array do share is in fact the special array syntax I mentioned
before - plus some functions that 'magically' work.
Refactoring existing functional code to OOP design patterns is a pain,
if not impossible, but desired to reduce development costs.
I hereby request some improvement on ArrayObject and its siblings, ie.
- a better integration with the already existing array_* functions, so
that ArrayObject and derivates can be considered real arrays.
- a richer set of predefined interfaces and utility classes like
ArrayAccess and Iterator that allow any implementing class to be used in
certain array context, eg. Sortable, Comparable, Mergable, Walkable ,
etc.
- maybe a collection interface just like in Java, with 'Array' being
the most versatile class -- implementing most of the interfaces.
...or maybe just some more 'magic functions' that allow the prior, but
cripple PHP's emerging OOP approach.
Reproduce code:
---------------
$ArrayObject = new ArrayObject();
$ArrayObject[] = 'foo';
$ArrayObject[] = 'bar';
$ArrayObject[] = 479;
sort($ArrayObject);
var_dump($ArrayObject);
$values = array_values($ArrayObject);
var_dump($values);
foreach($ArrayObject as $key => $value) {
var_dump($key, $value);
}
var_dump(is_array($ArrayObject));
Expected result:
----------------
string(6) "sorted"
object(ArrayObject)#1 (3) {
[0]=>
string(3) "bar"
[1]=>
string(3) "foo"
[2]=>
int(479)
}
string(6) "values"
array(3) {
[0]=>
string(3) "bar"
[1]=>
string(3) "foo"
[2]=>
int(479)
}
string(8) "is_array"
bool(true)
Actual result:
--------------
<b>Warning</b>: sort() expects parameter 1 to be array, object given
in <b>...</b><br />
string(6) "sorted"
object(ArrayObject)#1 (3) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
int(479)
}
<br />
<b>Warning</b>: array_values() [<a
href='function.array-values'>function.array-values</a>]: The argument
should be an array in <b>...</b><br />
string(6) "values"
NULL
string(8) "is_array"
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44734&edit=1