php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37134 PDOStatement::fetchObject calls constructor after populating fields
Submitted: 2006-04-19 09:29 UTC Modified: 2006-04-19 19:05 UTC
From: mark-phpbugs at vectrex dot org dot uk Assigned: hell (profile)
Status: Not a bug Package: PDO related
PHP Version: * OS: *
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mark-phpbugs at vectrex dot org dot uk
New email:
PHP Version: OS:

 

 [2006-04-19 09:29 UTC] mark-phpbugs at vectrex dot org dot uk
Description:
------------
PDOStatement::fetchObject calls constructor after populating fields, which is not what I expected.

I expected it to call __construct() first, then populate fields.

Reproduce code:
---------------
<?php
	/* Test case for PDOStatement::fetchObject('ClassName')
	 * Vars populated before constructor is run.
	 */
	
	$db = new PDO("sqlite::memory:");
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	
	$db->exec("CREATE TABLE person (id INTEGER NOT NULL, name varchar(100))");
	$db->exec("INSERT INTO person(id, name) VALUES (1, 'Fred')");
	
	class Person {
		var $id;
		var $name;
		function __construct() {
			$this->id = -1;
		}
	}
	
	$st = $db->query('SELECT * FROM person');
	$fred = $st->fetchObject('Person');
	
	print_r($fred);
?>


Expected result:
----------------
$fred->id should be 1

As the constructor will be called before variable population.

Actual result:
--------------
$fred->id is -1

Because the constructor is called after variable population.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-19 09:58 UTC] tony2001@php.net
Assigned to the maintainer.
 [2006-04-19 19:05 UTC] helly@php.net
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

This is by design.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 02:01:29 2024 UTC