php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38572 Destructor may be called before all references to it are gone
Submitted: 2006-08-23 19:40 UTC Modified: 2007-08-17 16:52 UTC
Votes:5
Avg. Score:4.2 ± 0.7
Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%)
Same OS:1 (20.0%)
From: richardkmiller at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: tested on Mac OS X and Windows
Private report: No CVE-ID: None
 [2006-08-23 19:40 UTC] richardkmiller at gmail dot com
Description:
------------
The documentation for PHP 5 destructors (http://php.net/
manual/en/language.oop5.decon.php) says "The destructor method 
will be called as soon as all references to a particular 
object are removed..." but this may not always be true.

In PHP 5.1, the mysqli object is destroyed before the 
destructor of my class which refers to it, so I can't make 
mysqli calls from my destructor. 


Reproduce code:
---------------
<?php

$db = new mysqli('localhost', USERNAME, PASSWORD, DATABASE);

class Test
{
	public function __construct()
	{
		global $db;
		$db->query("SELECT 'test'");
	}
	public function __destruct()
	{
		global $db;
		$db->query("SELECT 'test'");  // line 15
	}
}

$test = new Test();

?>


Expected result:
----------------
I expect this code to produce no errors (no output).

Actual result:
--------------
This code produces the following warning:

Warning: mysqli::query() [function.mysqli-query]: Couldn't 
fetch mysqli in test.php on line 15

This bug appears to have been documented at #31326 and #36034.  
If it isn't planned to be fixed, the documentation should be 
updated to read the following (quoting helly@php.net from 
#36034):

"You cannot rely on any order of destruction in shutdown 
sequence. All you can do is ensuring yourself that all related 
variables are set NULL or are being unset() prior to script 
end. If you do that in correct order then php calls the 
destructors in correct order."


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-17 16:52 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 23:01:32 2024 UTC