#49269 [Opn->Asn]: Ternary operator fails on Iterator object when used inside foreach declaration
| From: | colder@php.net | Date: | Mon, 17 Aug 2009 00:49:28 +0000 |
| Subject: | #49269 [Opn->Asn]: Ternary operator fails on Iterator object when used inside foreach declaration | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-141118@lists.php.net to get a copy of this message | ||
ID: 49269
Updated by: colder@php.net
Reported By: president at basnetworks dot net
-Status: Open
+Status: Assigned
Bug Type: Class/Object related
Operating System: All
PHP Version: 5.3.0
-Assigned To:
+Assigned To: colder
New Comment:
http://patches.colder.ch/Zend/bug49269.patch?markup
This should fix it, and passes all the Zend tests, but as it may affect
some untested edge cases out there, further checking is required.
Previous Comments:
------------------------------------------------------------------------
[2009-08-16 17:21:46] president at basnetworks dot net
sjoerd-php at linuxonly dot nl,
Your code sample is much clearer, and seems to narrow it down to the
ternary operator mis-behaving. Thanks for the added clarification, I
will update the report.
You should also "vote" that you reproduced the bug above.
------------------------------------------------------------------------
[2009-08-16 10:57:44] sjoerd-php at linuxonly dot nl
Note that the Iterator in my previous comment sucks and should not be
used.
------------------------------------------------------------------------
[2009-08-16 10:53:07] sjoerd-php at linuxonly dot nl
Thank you for your bug report.
I could reproduce the behavior and made a code sample to reproduce it:
<?php
class TestObject implements Iterator
{
private $first = true;
function valid()
{
if ($this->first)
{
$this->first = false;
return true;
}
return false;
}
function current() { }
function next() { }
function key() { }
function rewind() { }
}
$array_object = new TestObject();
// Without ternary operator, the foreach is entered
foreach ($array_object as $item)
{
echo "This works.\n";
}
// With ternary operator, the foreach is not entered
foreach ((true ? $array_object : $array_object) as $item)
{
die("Good. Expected behavior.\n");
}
die("Bad. Foreach was skipped.\n");
?>
------------------------------------------------------------------------
[2009-08-16 10:52:20] sjoerd-php at linuxonly dot nl
Thank you for your bug report.
I could reproduce the behavior and made a code sample to reproduce it:
<?php
class TestObject implements Iterator
{
private $first = true;
function valid()
{
if ($this->first)
{
$this->first = false;
return true;
}
return false;
}
function current() { }
function next() { }
function key() { }
function rewind() { }
}
$array_object = new TestObject();
// Without ternary operator, the foreach is entered
foreach ($array_object as $item)
{
echo "This works.\n";
}
// With ternary operator, the foreach is not entered
foreach ((true ? $array_object : $array_object) as $item)
{
die("Good. Expected behavior.\n");
}
die("Bad. Foreach was skipped.\n");
?>
------------------------------------------------------------------------
[2009-08-16 01:36:43] president at basnetworks dot net
After further testing, I have found this bug is stranger than it
seems:
foreach ((isset($array_object) ? $array_object : array('1', '2', '3'))
as $item)
{
echo $item;
}
Should either print 'abc' or '123' no matter if isset() is successful
or fails. It prints neither. Now I am wondering if it is not isset(),
but the ternary operator that is failing.
------------------------------------------------------------------------
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
http://bugs.php.net/49269
--
Edit this bug report at http://bugs.php.net/?id=49269&edit=1