php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #49538 ArrayAccess Force implement append
Submitted: 2009-09-12 22:16 UTC Modified: 2012-11-14 21:54 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: kevinpeno at gmail dot com Assigned:
Status: Wont fix Package: SPL related
PHP Version: 5.3.0 OS: ALL
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:
MUST BE VALID
Solve the problem:
37 + 31 = ?
Subscribe to this entry?

 
 [2009-09-12 22:16 UTC] kevinpeno at gmail dot com
Description:
------------
ArrayAccess should force the implementation of append to cover the case of $array[] = somevalue so that one can properly handle values appended in this manner. Additionally, most internal classes implementing ArrayAccess add their own append method anyway and this would provide some sort of naming standard.

Examples of conflicts in naming include ArrayObject::append() and SplDoublyLinkedList::push()

Reproduce code:
---------------
<?php
class MyArrayObject extends ArrayObject
{
	function append( $v )
	{
		//Should print the value if value appended
		var_dump( 'Append:', $value );
	}

	function offsetSet( $k, $v )
	{
		//Will Print OffsetSet: \n NULL \n NULL - See http://bugs.php.net/bug.php?id=49537
		var_dump( 'offsetSet:', $k, $value );
	}

}

$test = new MyArrayObject();
$test[] = 'test';
?>

Expected result:
----------------
MyArrayObject::append() called rather than MyArrayObject::offsetSet()

Actual result:
--------------
MyArrayObject::offsetSet() is called

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-12 22:18 UTC] kevinpeno at gmail dot com
Dumbass moment on the offsetSet bug. Please ignore that part :P The rest I still would like to see though :)
 [2010-11-24 14:46 UTC] jani@php.net
-Package: Feature/Change Request +Package: SPL related
 [2012-11-14 21:54 UTC] levim@php.net
Thank you for your input.  However, the current API allows you to override the 
append behavior. It may have been nice to have split these when they were created, 
but it has already been finished and it's not going to change.

function offsetSet($k, $v) {
    if ($k === NULL) {
        $this->append($v);
    } else {
        parent::offsetSet($k, $v);
    }
}
 [2012-11-14 21:54 UTC] levim@php.net
-Status: Open +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC