From:             president at basnetworks dot net
Operating system: All
PHP version:      5.3.0
PHP Bug Type:     Class/Object related
Bug description:  isset() fails on Iterator object when used inside foreach 
declaration

Description:
------------
The function isset() produces an incorrect result when used on an object
that implements the Iterator interface, within a foreach declaration.

As illustrated below, when used outside of the foreach() declaration,
isset() works as expected in the object that implements Iterator interface,
returning true, causing the output 'true'.

When isset() is used within the foreach() declaration on the same object,
it instead returns false, causing an empty array to be used for the
foreach() loop.

There is no reason the isset() function should return anything different
when used within the foreach() declaration block.

Reproduce code:
---------------
class NormalClass
{
        public $a, $b, $c;

        public function __construct()
        {
                $this->a = 'a';
                $this->b = 'b';
                $this->c = 'c';
        }
}

class ArrayClass implements Iterator
{
        private $internal_array;

        public function __construct()
        {
                $this->internal_array = array('a', 'b', 'c');
        }

        public function key()
        {
                return key($this->nodes);
        }

        public function current()
        {
                return current($this->nodes);
        }

        public function next()
        {
                next($this->nodes);
        }

        public function valid()
        {
                return (current($this->nodes) !== false) ? true : false;
        }

        public function rewind()
        {
                reset($this->nodes);
        }
}

$array = array('a', 'b', 'c');
$normal_object = new NormalClass();
$array_object = new ArrayClass();

echo "Array: " . (isset($array) ? 'true' : 'false');
echo "\nNormal Object: " . (isset($normal_object) ? 'true' : 'false');
echo "\nArray Object: " . (isset($array_object) ? 'true' : 'false');

echo "\nArray: ";
foreach ((isset($array) ? $array : array()) as $item)
{
        echo $item;
}

echo "\nNormal Object: ";
foreach ((isset($normal_object) ? $normal_object : array()) as $item)
{
        echo $item;
}

echo "\nArray Object: ";
foreach ((isset($array_object) ? $array_object : array()) as $item)
{
        echo $item;
}

Expected result:
----------------
Array: true
Normal Object: true
Array Object: true
Array: abc
Normal Object: abc
Array Object: abc

Actual result:
--------------
Array: true
Normal Object: true
Array Object: true
Array: abc
Normal Object: abc
Array Object: 

-- 
Edit bug report at http://bugs.php.net/?id=49269&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49269&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49269&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49269&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49269&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49269&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49269&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49269&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49269&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49269&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49269&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49269&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49269&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49269&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49269&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49269&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49269&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49269&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49269&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49269&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49269&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49269&r=mysqlcfg

Reply via email to