|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52444 Extending PDOStatement and replacing fetch() apply when using foreach
Submitted: 2010-07-26 12:57 UTC Modified: 2014-01-01 12:48 UTC
Avg. Score:4.8 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:3 (75.0%)
From: antennen at gmail dot com Assigned:
Status: Open Package: PDO SQLite
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: antennen at gmail dot com
New email:
PHP Version: OS:


 [2010-07-26 12:57 UTC] antennen at gmail dot com
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() {
	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");
foreach ($s as $r) {

Expected result:
array(3) {
  string(1) "1"
  string(4) "test"

Actual result:
array(2) {
  string(1) "1"
  string(4) "test"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-11-26 13:31 UTC]
PDO should rather implement the Iterator interface so one can override "current"
 [2014-01-01 12:48 UTC]
-Package: PDO related +Package: PDO SQLite
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC