php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52308 ArrayAccess does not automatically $obj[] = $x
Submitted: 2010-07-11 17:50 UTC Modified: 2010-07-12 12:35 UTC
From: ch@php.net Assigned: rquadling (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2010-07-11 17:50 UTC] ch@php.net
Description:
------------
When implementing the ArrayAccess Interface one could be tempted to think that
if "$obj[3] = $x" works, the common syntax "$obj[] = $x" would work as well.
But it doesn't. For this to work one has to enhance the offsetSet() method.

As it's only a one-liner I propose to put it into the example code on
http://de2.php.net/manual/en/class.arrayaccess.php

Expected result:
----------------
    public function offsetSet($offset, $value) {
        if ($offset == "") 
          $this->container[] = $value;
        else
          $this->container[$offset] = $value;    
    }

Actual result:
--------------
    public function offsetSet($offset, $value) {
        $this->container[$offset] = $value;
    }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-11 17:56 UTC] ch@php.net
var_dump() just showed me that the check should better be: 
   if ($offset === null)
 [2010-07-12 12:35 UTC] rquadling@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: rquadling
 [2010-07-12 12:36 UTC] rquadling@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.
 [2010-07-12 12:36 UTC] rquadling@php.net
Automatic comment from SVN on behalf of rquadling
Revision: http://svn.php.net/viewvc/?view=revision&revision=301180
Log: Show the use $obj[] within an ArrayAccess interface.

Fix bug#52308
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC