From: jaap dot taal at gmail dot com
Operating system: Windows AND ubuntu
PHP version: 5.2.5
PHP Bug Type: MySQL related
Bug description: mysql_fetch_object calls __construct too late
Description:
------------
I'm using the mysql_fetch_object's ability to create a custom object. The
constructor of the object initializes the .
The scripts simply gets all the data from the a table and uses
mysql_fetch_object to create the objects. The id property however is not
set, it is null.
I think that the mysql_fetch_object function first creates an object
setting properties and than it turns this object into an object of the
specified type, calling the constructor in the proces.
looking at the php source for mysql_fetch_object shows me that a hash is
retrieved first: php_mysql_fetch_hash
After that the hash is converted into an object:
object_and_properties_init
I think an object should be created calling its constructor and than after
that is done, the properties should be set.
Reproduce code:
---------------
<?php
/*
USE test;
DROP TABLE IF EXISTS `tbl_content`;
CREATE TABLE `tbl_content` (
`id` int(11) NOT NULL auto_increment,
`content` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `tbl_content` VALUES (1,'asdf1'),(2,'asdf2'),(3,'asdf3');
*/
$link = mysql_connect('localhost', 'test', 'test');
mysql_select_db('test');
class tbl_content {
var $id;
var $content;
function tbl_content() {
$this->id = null;
}
}
$result = mysql_query("SELECT * FROM tbl_content", $link);
if (!$result) {
die(mysql_error());
}
while ($obj = mysql_fetch_object($result, 'tbl_content')) {
var_dump($obj);
}
?>
Expected result:
----------------
object(tbl_content)#1 (2) {
["id"]=>
string(1) "1"
["content"]=>
string(5) "asdf1"
}
object(tbl_content)#2 (2) {
["id"]=>
string(1) "2"
["content"]=>
string(5) "asdf2"
}
object(tbl_content)#1 (2) {
["id"]=>
string(1) "3"
["content"]=>
string(5) "asdf3"
}
Actual result:
--------------
object(tbl_content)#1 (2) {
["id"]=>
NULL
["content"]=>
string(5) "asdf1"
}
object(tbl_content)#2 (2) {
["id"]=>
NULL
["content"]=>
string(5) "asdf2"
}
object(tbl_content)#1 (2) {
["id"]=>
NULL
["content"]=>
string(5) "asdf3"
}
--
Edit bug report at http://bugs.php.net/?id=44287&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=44287&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=44287&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=44287&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=44287&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=44287&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=44287&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=44287&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=44287&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=44287&r=support
Expected behavior: http://bugs.php.net/fix.php?id=44287&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=44287&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=44287&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=44287&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=44287&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=44287&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=44287&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=44287&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=44287&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=44287&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=44287&r=mysqlcfg