php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48487 fetch_object calls __set before constructor
Submitted: 2009-06-06 20:33 UTC Modified: 2010-01-04 10:31 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:1 (33.3%)
From: joel at purerave dot com Assigned:
Status: Closed Package: MySQLi related
PHP Version: 5.2.9 OS: win2k sp4
Private report: No CVE-ID: None
 [2009-06-06 20:33 UTC] joel at purerave dot com
Description:
------------
mysqli_fetch_object with custom class calls __set() before constructor.

Reproduce code:
---------------
<?php
$mysqli = new mysqli("localhost", "root", "root", "test");
class myData {
	function __construct($param) {
		echo 'creating'.PHP_EOL;
	}
	function __set($name, $value) {
		$this->{$name} = $value;
		echo 'setting'.PHP_EOL;
	}
}

$sql = "SELECT id FROM test LIMIT 1";
$result = $mysqli->query($sql);
while ($obj = $result->fetch_object('myData', array('data'))) {
}

Expected result:
----------------
creating
setting


Actual result:
--------------
setting
creating


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-25 17:14 UTC] joel at purerave dot com
No response yet?
 [2009-11-06 16:29 UTC] caferrari at gmail dot com
Help!.. i am having this issue with php 5.2.10 on Ubuntu 9.10 and Debian 5... and its evil!... my solution:

class Exemplo {
	public function __construct($id=0, $nome='', $sigla=''){
		if (isset($this->id)) return; //Ugly solution!!! help!

		$this->id	= $id;
		$this->nome 	= $nome;
		$this->sigla 	= $sigla;
	}
}
 [2010-01-04 10:31 UTC] uw@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Fixed with http://news.php.net/php.cvs/61460  
 [2010-05-12 09:51 UTC] vadimx at gmail dot com
In mysql_fetch_object the same bug.
 [2014-04-28 14:51 UTC] ircmaxell@php.net
Note that this is NOT fixed in any release. The change was reverted.

Just leaving this note here so that anyone who stumbles on it in the future will not go chasing their tails.
 [2015-10-01 13:33 UTC] grell at werk4 dot net
ircmaxwell@php.net:
Are you sure that this has never been released?
In PHP 5.2.17 I see this exact behaviour.

Testcase:
<?php
class TestClass
{
	public function __construct() {
		echo 'construct'."\n";
	}
	
	public function __set($key, $value) {
		echo 'set "'.$key.'" to "'.$value . '"' ."\n";
	}

}
$db = new mysqli();
$db->query('SELECT "PHP" as name')->fetch_object('TestClass');

Expected output (as documented):
set "name" to "PHP"
construct

Actual output:
construct
set "name" to "PHP"
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC