Bug #54964 [NEW]: PDO does not use defined class, but instead returns anonymous class
From:
Operating system:
PHP version: 5.3.6
Package: PDO related
Bug Type: Bug
Bug description:PDO does not use defined class, but instead returns anonymous class
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 bug report at http://bugs.php.net/bug.php?id=54964&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=54964&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=54964&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=54964&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=54964&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=54964&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=54964&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=54964&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=54964&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=54964&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=54964&r=support
Expected behavior: http://bugs.php.net/fix.php?id=54964&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=54964&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=54964&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=54964&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=54964&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=54964&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=54964&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=54964&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=54964&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=54964&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=54964&r=mysqlcfg
Thread (4 messages)
- daveb at bulmore dot net