|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-06-02 07:04 UTC] daveb at bulmore dot net
[2014-01-01 12:44 UTC] felipe@php.net
-Package: PDO related
+Package: PDO MySQL
[2019-11-05 15:42 UTC] camporter1 at gmail dot com
[2020-03-13 13:57 UTC] cmb@php.net
-Status: Open
+Status: Not a bug
-Assigned To:
+Assigned To: cmb
[2020-03-13 13:57 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 20:00:01 2025 UTC |
Description: ------------ PDO fetch object methods do not use the class defined: // not used, but should be class User { public $username; public $first_name; public $last_name; public $created; } $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); $obj = $stmt->fetch(); //returns anonymous class but instead use a generated anonymous class based on the select statement. Test script: --------------- <pre> <?php class User { public $username; public $first_name; public $last_name; public $created; } $dbh = new PDO("mysql:host=localhost;dbname=testdb;", "test", "test"); $stmt = $dbh->query('select * from users'); $stmt->setFetchMode(PDO::FETCH_CLASS, 'User'); foreach ($stmt as $obj) //or while ($obj = $stmt->fetch()) { echo $obj->username, " ", $obj->first_name, " ", $obj->last_name, " ", $obj->role, " ", $obj->verified, " ", $obj->trusted, " ", $obj->created, "\n"; } $dbh = null; ?> </pre> <!-- CREATE TABLE `users` ( `username` varchar(40) NOT NULL, `password` varchar(40) NOT NULL, `first_name` varchar(40) DEFAULT NULL, `last_name` varchar(60) DEFAULT NULL, `email` varchar(255) DEFAULT NULL, `role` varchar(20) NOT NULL DEFAULT 'user', `verified` varchar(50) NOT NULL DEFAULT 'false', `trusted` varchar(5) NOT NULL DEFAULT 'false', `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`username`) ); --> Expected result: ---------------- Should get an instance of: class User { public $username; public $first_name; public $last_name; public $created; } and an error when trying to access the properties: $obj->role $obj->verified $obj->trusted Actual result: -------------- Returns an anonymous class with anything that's defined in the select statement.