Bug #76921 [Opn]: ArrayAccess only supports increment on by-ref offsetGet()
Edit report at https://bugs.php.net/bug.php?id=76921&edit=1
ID: 76921
Updated by: cmb@php.net
Reported by: nikic@php.net
Summary: ArrayAccess only supports increment on by-ref
offsetGet()
Status: Open
Type: Bug
Package: Scripting Engine problem
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
For reference: <https://3v4l.org/EFpo1>.
Previous Comments:
------------------------------------------------------------------------
[2018-09-23 14:11:13] nikic@php.net
Description:
------------
Overloaded properties implement $x->y++ by calling __set('y', __get('y') +
1).
The same does not work for ArrayAccess. ArrayAccess only supports $x[0]++ if offsetGet() returns by
reference, in which case the increment is performed on the reference.
This is inconsistent both with the overloaded property behavior and with compound assignment
operations on ArrayAccess. That is, $x[0] += 1 *does* work with a by-value offsetGet().
Test script:
---------------
<?php
class Test implements ArrayAccess {
public function __get($k) {
return 1;
}
public function __set($k, $v) {
echo "__set($v)\n";
}
public function offsetGet($k) {
return 1;
}
public function offsetSet($k, $v) {
echo "offsetSet($v)\n";
}
public function offsetExists($k) {}
public function offsetUnset($k) {}
}
$t = new Test;
echo "Property:\n";
$t->x++;
echo "Offset:\n";
$t[0]++;
Expected result:
----------------
Property:
__set(2)
Offset:
offsetSet(2)
Actual result:
--------------
Property:
__set(2)
Offset:
Notice: Indirect modification of overloaded element of Test has no effect
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76921&edit=1
Thread (2 messages)