|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-06-18 13:00 UTC] dan at yes dot lt
Description:
------------
method ArrayAccess actualy works wrong with isset()/empty()...
isset()/empty() now calls only ArrayAccess::offsetExist, but should call both ArrayAccess::offsetExist and ArrayAccess::offsetGet to check if value is realy set.
that's how arrays do...
Reproduce code:
---------------
class Test implements ArrayAccess
{
protected $_array = array();
protected $_count = 0;
function offsetExists($offset)
{
return $offset >= 0 && $offset < $this->_count;
}
function offsetGet($offset)
{
return $this->_array[$offset];
}
function offsetSet($offset, $value)
{
$this->_array[] = $value;
$this->_count++;
}
function offsetUnset($offset)
{
unset($this->_array[$offset]);
}
}
$a = new Test;
$a[] = 'A'; // 0
$a[] = 10; // 1
$a[] = null; // 2
echo isset($a[0]) ? 'set' : 'unset', "\n";
echo isset($a[1]) ? 'set' : 'unset', "\n";
echo isset($a[2]) ? 'set' : 'unset', "\n";
Expected result:
----------------
set
set
unset
Actual result:
--------------
set
set
set
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 19:00:01 2025 UTC |
ok, now think... if you have method to work with array or any object with ArrayAccess implemented... function some($list) { if (isset($list[0])) { // do something with $list[0] return true; } else { // no $list[0] defined, skipping return false; } } user, who implements this function must not know the real type of the $list variable - but only that this variable can be accessed as array... now - if $list = array(0 => null), then this function works correctly, but if $list = new ArrayObject(array(0 => null)), then it fails... now, you say, i must to use offsetGet method... then, i must check whether $list is instance of ArrayAccess, then add complex checkings using offsetExists and offsetGet... was the ArrayAccess realy planned to be such a silly structure to use it in such complex way?.. I don't think so. I still think, isset() must automaticaly call offsetGet after offsetExists returns true...