|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-04-19 09:58 UTC] tony2001@php.net
[2006-04-19 19:05 UTC] helly@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 19:00:02 2025 UTC |
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.