note 88079 deleted from class.arrayaccess by joey
| From: | joey@php.net | Date: | Wed, 10 Oct 2012 01:32:11 +0000 |
| Subject: | note 88079 deleted from class.arrayaccess by joey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-191226@lists.php.net to get a copy of this message | ||
Note Submitter: AryehGregor+php-comment at gmail dot com
----
Note that at least in PHP 5.1, objects implementing ArrayAccess cannot return objects by reference.
See http://bugs.php.net/bug.php?id=34783 . If
you have code like
<?php
$x = &$y[0];
?>
then this will (as far as I can tell) *always* fail unless $y is a real array -- it cannot work if
$y is an object implementing ArrayAccess. If your offsetGet() function returns by reference, you
get the fatal error "Declaration of MyClass::offsetGet() must be compatible with that of
ArrayAccess::offsetGet()". If you try to have it return by value, however, you get the
(contradictory) fatal error "Objects used as arrays in post/pre increment/decrement must return
values by reference", at least in my version of PHP.
It is therefore not possible to take arbitrary code dealing with arrays and try to substitute an
object of your own for an array, even if all of the normal array functions didn't fail as well
(which they do, or at least some of them).