Edit report at https://bugs.php.net/bug.php?id=32983&edit=1
ID: 32983
Comment by: charles dot louw at gmail dot com
Reported by: jason at amp-design dot net
Summary: Bogus error message when using ArrayAccess /
References
Status: Closed
Type: Bug
Package: SPL related
Operating System: *
PHP Version: 5.0.3
Assigned To: helly
Block user comment: N
Private report: N
New Comment:
Excuse me if I'm being obtuse here but this doesn't really solve the problem
and renders the ArrayAccess interface useless for anything other than
1-dimensional arrays. So you can't do:
$data = new ArrayAccessImpl(); // using class in example above
$data['level_1'] = array();
$data['level_1']['level_2'] = 'some data';
Suggestion:
1. Change the ArrayAccess interface to support references - so as to fix the
problem.
2. Reduce the severity of the error triggered for class declarations that don't
implement the ArrayAccess methods with references from "fatal" to "warning" (so
that the application does not terminate).
3. If I'm not amiss very little will need to be changed in the SPL to then
handle data passed to & from the (ArrayAccess) methods whether passed by
reference or not. This will "maintain" BC.
4. Once the old implementation of the ArrayAccess methods have been deprecated
you can remove the interface check specific to ArrayAccess which will then
revert to a fatal.
* The fatal error I'm talking about would be: "Declaration of
ArrayAccessImpl::offsetSet() must be compatible with that of
ArrayAccess::offsetSet()"... and for the other methods.
* You could do this as a major revision. I would rather know that I need to
update the classes that implement ArrayAccess before upgrading to say PHP.v6
rather than have to live with this limitation. Basically I will now not use
ArrayAccess again because of this bug.
Previous Comments:
------------------------------------------------------------------------
[2005-08-16 23:30:14] [email protected]
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php
We found out that this is not solvable without blowing up the interface and
creating a BC or providing an additional interface to support references and
thereby creating an internal nightmare - actually i don't see a way we can make
that work ever. Thus we decided to enforce the original design and disallow
references completley. Also the error message should have changed with 5.0.4 or
is being changed with 5.0.5/5.1 at least
------------------------------------------------------------------------
[2005-08-16 07:44:50] sslotnick at gmail dot com
I have a similar but different reproduction using two dimensional arrays
instead of explicitly setting a reference.
class ArrayAccessImpl implements ArrayAccess {
private $data = array();
public function offsetUnset($index) {}
public function offsetSet($index, $value) {
$this->data[$index] = $value;
}
public function offsetGet($index) {
return $this->data[$index];
}
public function offsetExists($index) {
return isset($this->data[$index]);
}
}
$data = new ArrayAccessImpl();
$data['element'] = array();
$data['element']['element2'] = "hi";
------------------------------------------------------------------------
[2005-05-09 19:05:42] [email protected]
Actually at the moment this is a known issue which we cannot fix appropriate
right away. But we are working on the matter.
------------------------------------------------------------------------
[2005-05-09 12:38:42] jason at amp-design dot net
Description:
------------
I'm not 100% sure this is considered a "bug" as such, anyway, I thought I'd
point it out, and let you decide. It's more a case of the error message being a
little fuzzy.
When trying to assign an item by reference by using the reference operator, &,
to an element inside a class that implements ArrayAccess produces a werid error
message.
Admittedly, the code I've provided is probably not valid PHP code, because the
nature of the ArrayAccess interface means that data is assigned and returned
from offsetSet and offsetGet by value, so using refernces should probably not
work.
However, the when you do try this, you get an error about about post/pre
increment/decrement. I'm not sure what this refers to, but it doesn't seem to
be very descriptive.
Reproduce code:
---------------
<?php
class ArrayAccessImpl implements ArrayAccess {
private $data = array();
public function offsetUnset($index) {}
public function offsetSet($index, $value) {
$this->data[$index] = $value;
}
public function offsetGet($index) {
return $this->data[$index];
}
public function offsetExists($index) {
return isset($this->data[$index]);
}
}
$data = new ArrayAccessImpl();
$test = 'some data';
$data['element'] = &$test;
?>
Expected result:
----------------
Unsure, probably an error message relating to the fact ArrayAccess objects can
not assign by reference
Actual result:
--------------
Fatal error: Objects used as arrays in post/pre increment/decrement must return
values by reference
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=32983&edit=1