php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80670 error call exit in construct not run destruct
Submitted: 2021-01-25 21:26 UTC Modified: 2021-01-25 21:29 UTC
From: icarosnet at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 8.0Git-2021-01-25 (Git) OS: all
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: icarosnet at gmail dot com
New email:
PHP Version: OS:

 

 [2021-01-25 21:26 UTC] icarosnet at gmail dot com
Description:
------------
after few test; i try to migrate from php 7.4 to 8 and get that i think is a bug or you not documented it:

in php 7.4 when i run the constructor and inside it i run a exit(), the destructor will triggered.

in php 8.0 not; check this link example:

Work in php 7.4.13
http://sandbox.onlinephpfunctions.com/code/38f312d12f72cba32c3cd1819a047504b0d62f6c

not work in php 8.0
http://sandbox.onlinephpfunctions.com/code/38f312d12f72cba32c3cd1819a047504b0d62f6c



Test script:
---------------
<?php
//run in php 7.4.13 and php 8.0 you would see the diference.
class Test{
    function __construct(){
        echo '<div>Star Construct</div><br>';
        if(true){
            exit();
        }
    }
    function __destruct(){
        echo '<div>Run Destruct</div><br>';
    }
}
$t = new Test();


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-01-25 21:29 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-01-25 21:29 UTC] nikic@php.net
This is explicitly mentioned in the migration guide (https://www.php.net/manual/en/migration80.incompatible.php):

> If an object constructor exit()s, the object destructor will no longer be called. This matches the behavior when the constructor throws.
 [2021-01-25 21:30 UTC] daverandom@php.net
I doubt this would be considered a bug. I would not expect the destructor to be run if the constructor has not completed, and would argue the change in behaviour was actually a bug fix.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 09:01:26 2025 UTC