php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29897 $array[] syntax does not work with ArrayAccess implementation.
Submitted: 2004-08-30 16:10 UTC Modified: 2004-08-30 20:38 UTC
From: sb at sebastian-bergmann dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sb at sebastian-bergmann dot de
New email:
PHP Version: OS:

 

 [2004-08-30 16:10 UTC] sb at sebastian-bergmann dot de
Description:
------------
$array[] syntax does not work with ArrayAccess implementation.

Reproduce code:
---------------
<?php
class TestArray implements ArrayAccess {
    private $array = array();

    public function offsetExists($offset) {
        return isset($this->array[$offset]);
    }

    public function offsetGet($offset) {
        return $this->array[$offset];
    }

    public function offsetSet($offset, $value) {
        $this->array[$offset] = $value;
    }

    public function offsetUnset($offset) {
        unset($this->array[$offset]);
    }
}

$test = new TestArray;

// Does not work.
$test[] = 'Hello'; $test[] = ' World!';

// Works.
// $test[0] = 'Hello'; $test[1] = ' World!';

print $test[0] . $test[1];
?>

Expected result:
----------------
Hello World!

Actual result:
--------------
Notice: Undefined offset:  0 in D:\test2.php on line 10
Notice: Undefined offset:  1 in D:\test2.php on line 10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-30 20:38 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Your code is wrong, look @ array_access_007.phpt
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 17:01:34 2025 UTC