Bug #52444 [NEW]: Extending PDOStatement and replacing fetch() apply when using foreach

From: Date: Mon, 26 Jul 2010 10:57:40 +0000
Subject: Bug #52444 [NEW]: Extending PDOStatement and replacing fetch() apply when using foreach
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-152791@lists.php.net to get a copy of this message
From: Operating system: All PHP version: Irrelevant Package: PDO related Bug Type: Bug Bug description:Extending PDOStatement and replacing fetch() apply when using foreach Description: ------------ Extending PDOStatement and replacing fetch() doesn't work when iterating using foreach. It calls the built in fetch rather than my overridden one. This can be circumvented using while($r = $s->fetch()) { .. } but I believe this behaviour is unexpected. Test script: --------------- class ExtendedStatement extends PDOStatement { protected function __construct() { $this->setFetchMode(PDO::FETCH_ASSOC); } public function fetch($fetch_style = PDO::FETCH_ASSOC, $cursor_orientation = PDO::FETCH_ORI_NEXT, $cursor_offset = 0) { $r = parent::fetch($fetch_style, $cursor_orientation, $cursor_offset); if (is_array($r)) { $r["extradata"] = TRUE; } return $r; } } $db = new PDO("sqlite::memory:"); $db->setAttribute(PDO::ATTR_STATEMENT_CLASS, array("ExtendedStatement", array($db))); $db->exec("CREATE TABLE example(id INTEGER PRIMARY KEY, name VARCHAR)"); $db->exec("INSERT INTO example(name) VALUES('test')"); $s = $db->prepare("SELECT * FROM example"); $s->execute(); foreach ($s as $r) { var_dump($r); } Expected result: ---------------- array(3) { ["id"]=> string(1) "1" ["name"]=> string(4) "test" ["extradata"]=> bool(true) } Actual result: -------------- array(2) { ["id"]=> string(1) "1" ["name"]=> string(4) "test" } -- Edit bug report at http://bugs.php.net/bug.php?id=52444&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52444&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52444&r=trysnapshot53 Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52444&r=trysnapshottrunk Fixed in SVN: http://bugs.php.net/fix.php?id=52444&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52444&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=52444&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=52444&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=52444&r=needscript Try newer version: http://bugs.php.net/fix.php?id=52444&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=52444&r=support Expected behavior: http://bugs.php.net/fix.php?id=52444&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=52444&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=52444&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=52444&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52444&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=52444&r=dst IIS Stability: http://bugs.php.net/fix.php?id=52444&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=52444&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=52444&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=52444&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=52444&r=mysqlcfg

« previous php.bugs (#152791) next »