php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46105 mysql_fetch_object calls constructor on object after setting up properties
Submitted: 2008-09-17 12:35 UTC Modified: 2008-11-03 16:15 UTC
Votes:11
Avg. Score:4.5 ± 0.7
Reproduced:9 of 10 (90.0%)
Same Version:1 (11.1%)
Same OS:4 (44.4%)
From: ninzya at inbox dot lv Assigned: mysql
Status: Wont fix Package: MySQL related
PHP Version: 5.3.0alpha2 OS: Windows XP
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-09-17 12:35 UTC] ninzya at inbox dot lv
Description:
------------
when using custom object return through mysql_fetch_object, function allocates specified in second parameter object, sets up all properties and then calls constructor. I think this is wrong. Newly instantiated object's constructor must be called before any other operation on the object is performed.

Reproduce code:
---------------
/**
 * Object class
 *
 */
class Object {
  
  /**
   * Array of properties
   *
   * @var array
   */
  protected $_props =array();
  
  /**
   * Construct object
   *
   * @param array $props
   */
  public function __construct( $props =array()) {
    var_dump( 'constr');
    $this->_props =$props;
  }
  
  /**
   * Magic method override
   *
   * @param string $key
   */
  public function __isset( $key) {
    var_dump( 'isset');
    return array_key_exists( $key, $this->_props);
  }
  
  /**
   * Magic method override
   *
   * @param string $key
   * @return mixed/null
   */
  public function __get( $key) {
    var_dump( 'get');
    if( !array_key_exists( $key, $this->_props))
      return null;// entry does not exist
    // return obtained value
    return $this->_props[ $key];
  }
  
  /**
   * Magic method override
   *
   * @param string $key
   * @param mixed $value
   */
  public function __set( $key, $value) {
    var_dump( 'set');
    $this->_props[ $key] =$value;
  }
  
  /**
   * Magic method override
   *
   * @param string $key
   */
  public function __unset( $key) {
    var_dump( 'unset');
    unset( $this->_props[ $key]);
  }
  
  /**
   * Get associated array
   *
   * @return array
   */
  public function __invoke() {
    var_dump( 'invoke');
    return $this->_props;
  }
  
  /**
   * Get object name
   *
   * @return string
   */
  public function __toString() {
    return __CLASS__;
  }
  
}

......
mysql_fetch_object( $result, 'Object');

Expected result:
----------------
string(6) "constr"
string(3) "set"
string(3) "set"
string(3) "set"
string(3) "set"


Actual result:
--------------
string(3) "set"
string(3) "set"
string(3) "set"
string(3) "set"
string(6) "constr"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-03 15:48 UTC] johannes@php.net
Yes, the behavior  can be considered wrong, unfortunately we can't change it as code might rely on that order, like doing work with the data in the constructor ...
 [2008-11-03 16:15 UTC] ninzya at inbox dot lv
Well, then you should't fix any bugs at all and have bugtracker, because some folk's code may rely on those bugs. This is not a valid behavior, you HAVE TO fix this, and if necessary, warn users about the change. If this "feature" is not documented, there should be a slight warning about this.
 [2011-11-29 15:40 UTC] mohammed dot tarek at hotmail dot com
how this bug can happen 

this function can be useful but with this strange behavior of the function it 
can't used
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 04:02:11 2014 UTC