|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54964 PDO does not use defined class, but instead returns anonymous class
Submitted: 2011-05-31 16:34 UTC Modified: 2020-03-13 13:57 UTC
From: daveb at bulmore dot net Assigned: cmb (profile)
Status: Not a bug Package: PDO MySQL
PHP Version: 5.3.6 OS:
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: daveb at bulmore dot net
New email:
PHP Version: OS:


 [2011-05-31 16:34 UTC] daveb at bulmore dot net
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:
  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;

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',
  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:


Actual result:
Returns an anonymous class with anything that's defined in the select statement.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-02 07:04 UTC] daveb at bulmore dot net
It appears that the PDO methods return the class, but may also be proxying values from the database.  So there may or may not be a problem.
 [2014-01-01 12:44 UTC]
-Package: PDO related +Package: PDO MySQL
 [2019-11-05 15:42 UTC] camporter1 at gmail dot com
The behavior here seems to be that when fetching a class, there's no concern for whether columns match properties defined on the class. The rows are proper classes of the object, it's just that additional columns will be added to the objects as undefined properties, which PHP allows.

You can verify this by reflecting on a fetched row and verifying that the class matches.
 [2020-03-13 13:57 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-03-13 13:57 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Like camporter1 said (but s/undefined properties/undeclared properties).
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Sep 30 14:05:54 2022 UTC