php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36831 Trying to create a file in destructor fails
Submitted: 2006-03-23 12:24 UTC Modified: 2006-03-23 12:51 UTC
From: felho at avalon dot aut dot bme dot hu Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: felho at avalon dot aut dot bme dot hu
New email:
PHP Version: OS:

 

 [2006-03-23 12:24 UTC] felho at avalon dot aut dot bme dot hu
Description:
------------
In case when an object is freed up at the end of the script file creation will fail in destructor. When I force this before the end of script the file will be created.

Reproduce code:
---------------
<?php
	class foo {
		function __construct()
		{
			fopen(date('YmdHim').'.const.txt', 'w');
			mail('felho@x.hu', date('YmdHim').'.const.txt', '');
		}

		function __destruct()
		{
			fopen(date('YmdHim').'.dest.txt', 'w');
			mail('felho@x.hu', date('YmdHim').'.dest.txt', '');
		}
	}

	$foo = new foo();
	//$foo = 1;
?>

Expected result:
----------------
Creating two files and sending two mails.

Actual result:
--------------
Only one file was created but two mails was sent. If you remove the comment on the last line, the second file was created too.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-23 12:34 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Most parts of php are already shutdown when the engine calls the destructors after the script ends. For the same reason you cannot output anything from a destructor.
 [2006-03-23 12:51 UTC] felho at avalon dot aut dot bme dot hu
"Most parts of php are already shutdown when the engine calls the destructors after the script ends."
My first thought was to choose documentation related bug. This is not mentioned in the manual on the page which is related to __destruct.

"For the same reason you cannot output anything from a destructor."
It is funny, that the example in the manual exactly do this.
function __destruct() {
    print "Destroying " . $this->name . "\n";
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 09 10:01:27 2024 UTC