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
 [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: Tue Apr 16 23:01:30 2024 UTC