Bug #41727 [Bgs]: ArrayAccess::offsetExists works wrong with isset()
| From: | stas@php.net | Date: | Wed, 28 Sep 2011 17:48:48 +0000 |
| Subject: | Bug #41727 [Bgs]: ArrayAccess::offsetExists works wrong with isset() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163958@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=41727&edit=1
ID: 41727
Updated by: stas@php.net
Reported by: dan at yes dot lt
Summary: ArrayAccess::offsetExists works wrong with isset()
Status: Bogus
Type: Bug
Package: SPL related
Operating System: WinXP
PHP Version: 5.2.3
Block user comment: N
Private report: N
New Comment:
What prevents you from implementing offsetExists with any checks you like,
including null check?
Previous Comments:
------------------------------------------------------------------------
[2007-06-22 10:35:20] dan at yes dot lt
I see... you make php for yourself, but not for others... you don't listen for arguments, just
skipping the bugs... without any reason explanations...
thank you for good support and correct php!
------------------------------------------------------------------------
[2007-06-22 10:29:15] johannes@php.net
.
------------------------------------------------------------------------
[2007-06-22 07:46:23] dan at yes dot lt
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...
------------------------------------------------------------------------
[2007-06-22 07:34:01] johannes@php.net
Yes, I still think so.
------------------------------------------------------------------------
[2007-06-22 04:38:24] dan at yes dot lt
isset Determine whether a variable is set
[http://php.net/isset]
array_key_exists Checks if the given key or index exists in the array
[http://php.net/array-key-exists]
<?php
$a = array(0 => 10, 1 => null);
// so...
var_dump(isset($a[0])); // bool(true)
var_dump(array_key_exists(0, $a)); // bool(true)
// but...
var_dump(isset($a[1])); // bool(false)
var_dump(array_key_exists(0, $a)); // bool(true)
?>
now...
ArrayObject::offsetExists Returns whether the requested $index exists
[http://php.net/ArrayObject-offsetExists]
so, offsetExists must return the same as array_key_exists... but how isset() must work with
ArrayAccess?..
<?php
$a = new ArrayObject(array(0 => 10, 1 => null));
// so...
var_dump(isset($a[0])); // bool(true)
var_dump($a->offsetExists(0)); // bool(true)
var_dump(array_key_exists(0, $a)); // bool(true)
// but...
var_dump(isset($a[1])); // bool(true) | false expected
var_dump($a->offsetExists(1)); // bool(true)
var_dump(array_key_exists(1, $a)); // bool(true)
?>
in this case isset() returns true, but obviously must return false... don't you think so?..
isn't this situation silly?..
and do you still think - "this is not a bug"?..
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=41727
--
Edit this bug report at https://bugs.php.net/bug.php?id=41727&edit=1