php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72656 Object destructor called before constructor
Submitted: 2016-07-23 05:29 UTC Modified: 2021-08-27 11:11 UTC
From: joe88 dot ds at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.5.38 OS: Windows 7 x64
Private report: No CVE-ID: None
 [2016-07-23 05:29 UTC] joe88 dot ds at gmail dot com
Description:
------------
Calling exit from a user error handler causes an object to be destructed, even if
the object was never constructed.

Test script:
---------------
http://pastebin.com/UeBBj1hK

Expected result:
----------------
The Tree's destructor should never be called.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-25 09:48 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2016-07-25 09:48 UTC] cmb@php.net
There doesn't even need to be an error handler involved, see
<https://3v4l.org/cR3nF>. Looking at the opcodes[1] hints at
what's going on behind the scenes: first NEW is executed, and only
after that the arguments for the constructor are evaluated, which,
in this case, cause the script to terminate, without actually
calling the constructor. On shutdown, however, the newly created
object gets properly destroyed by calling its destructor.

In my opinion, this is a bug.

[1] <https://3v4l.org/cR3nF/vld#tabs>
 [2021-08-27 11:11 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: +Assigned To: cmb
 [2021-08-27 11:11 UTC] cmb@php.net
No, this is not a bug.  Actually, the behavior is mostly
documented[1]:

| Classes which have a constructor method call this method on each
| newly-created object, […]

and[2]:

| The destructor will be called even if script execution is
| stopped using exit().

The only missing peace to the puzzle is eager evaluation of
function arguments, which doesn't appear to be documented yet.

[1] <https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor>
[2] <https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor>
 [2021-08-27 11:12 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/06126805f490aba6f6c107980f673eada0c79e6c
Log: Fix #72656: Object destructor called before constructor
 [2021-08-27 11:12 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC