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

 ID:                 46105
 Comment by:         mohammed dot tarek at hotmail dot com
 Reported by:        ninzya at inbox dot lv
 Summary:            mysql_fetch_object calls constructor on object after
                     setting up properties
 Status:             Wont fix
 Type:               Bug
 Package:            MySQL related
 Operating System:   Windows XP
 PHP Version:        5.3.0alpha2
 Assigned To:        mysql
 Block user comment: N
 Private report:     N

 New Comment:

how this bug can happen 

this function can be useful but with this strange behavior of the function it 
can't used


Previous Comments:
------------------------------------------------------------------------
[2008-11-03 16:15:15] 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.

------------------------------------------------------------------------
[2008-11-03 15:48:53] johan...@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-09-17 12:35:52] 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"


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



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

Reply via email to