ID:               41727
 User updated by:  dan at yes dot lt
 Reported By:      dan at yes dot lt
-Status:           Bogus
+Status:           Open
 Bug Type:         SPL related
 Operating System: WinXP
 PHP Version:      5.2.3
 New Comment:

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"?..


Previous Comments:
------------------------------------------------------------------------

[2007-06-21 21:55:36] [EMAIL PROTECTED]

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

You can call offsetGet() yourself if you want.

------------------------------------------------------------------------

[2007-06-18 13:00:54] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=41727&edit=1

Reply via email to