php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46624 Fetching results using PDO::FETCH_CLASS set attributes before calling ctor
Submitted: 2008-11-19 20:14 UTC Modified: 2008-11-19 23:48 UTC
From: toby@php.net Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.2.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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: toby@php.net
New email:
PHP Version: OS:

 

 [2008-11-19 20:14 UTC] toby@php.net
Description:
------------
If you set the PDO fetch mode to PDO::FETCH_CLASS and fetch all results from a result set (aka processed statement), the constructor is called after the properties of the newly created object are set.

This is incorrect from the OO point of view, since the ctor may provide sensible default values for properties and prepare the object overall. Therefore property manipulation should happen after the ctor was called.

Reproduce code:
---------------
$db = $this->getTrackbackDbConection();
$select = $db->query( 'SELECT ... ;' );
$select->setFetchMode( PDO::FETCH_CLASS, 'myCustomClass' );
$result = $select->fetchAll();

class myCustomClass
{

    public $someProperty;

    public function __construct()
    {
       $this->someProperty = 'default'
    }
}


Expected result:
----------------
myCustomClass->$someProperty is overwritten by a value from the database.

Actual result:
--------------
myCustomClass->$someProperty is overwritten by 'default' since __construct() is called after the properties have been set in PDO.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-19 23:48 UTC] iliaa@php.net
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

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 04:01:30 2024 UTC