|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 05:00:01 2025 UTC |
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); } }