Bug #54964 [Opn->Nab]: PDO does not use defined class, but instead returns anonymous class

From: Date: Fri, 13 Mar 2020 13:57:29 +0000
Subject: Bug #54964 [Opn->Nab]: PDO does not use defined class, but instead returns anonymous class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226091@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54964&edit=1

 ID:                 54964
 Updated by:         cmb@php.net
 Reported by:        daveb at bulmore dot net
 Summary:            PDO does not use defined class, but instead returns
                     anonymous class
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            PDO MySQL
 PHP Version:        5.3.6
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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

Like camporter1 said (but s/undefined properties/undeclared properties).


Previous Comments:
------------------------------------------------------------------------
[2019-11-05 15:42:47] 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.

------------------------------------------------------------------------
[2011-06-02 07:04:41] 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.

------------------------------------------------------------------------
[2011-05-31 16:34:54] daveb at bulmore dot net

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.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=54964&edit=1


Thread (4 messages)

« previous php.bugs (#226091) next »