|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76921 ArrayAccess only supports increment on by-ref offsetGet()
Submitted: 2018-09-23 14:11 UTC Modified: 2020-11-17 12:53 UTC
From: Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
39 + 32 = ?
Subscribe to this entry?

 [2018-09-23 14:11 UTC]
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:

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";
echo "Offset:\n";

Expected result:

Actual result:
Notice: Indirect modification of overloaded element of Test has no effect


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-17 12:53 UTC]
For reference: <>.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun May 28 21:03:43 2023 UTC