php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73217 Finally block not executed
Submitted: 2016-10-01 16:33 UTC Modified: 2016-10-03 10:14 UTC
From: thekid@php.net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.0RC3 OS: Windows
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: thekid@php.net
New email:
PHP Version: OS:

 

 [2016-10-01 16:33 UTC] thekid@php.net
Description:
------------
There is a behavior change from PHP 7.0 -> PHP 7.1 with how finally blocks get executed

Test script:
---------------
<?php

function test() {
  $r= [];
  try {
    $r[]= 'Try';
    throw new Exception('Error');
  } catch (Exception $e) {
    $r[]= 'Catch'; 
    return $r;
  } finally {
    $r[]= 'Finally';
  }
}

var_dump(test());

Expected result:
----------------
array(3) {
  [0]=>
  string(3) "Try"
  [1]=>
  string(5) "Catch"
  [2]=>
  string(7) "Finally"
}

Actual result:
--------------
array(2) {
  [0]=>
  string(3) "Try"
  [1]=>
  string(5) "Catch"
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-01 16:47 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-10-01 16:47 UTC] nikic@php.net
The new behavior is correct. "return $r" is what determines the return value, and it is not relevant if $r is modified afterwards. Due to a bug this was not enforced previously in certain cases.

(This applies also to by-reference returns in that the returned reference is fixed. This means that the value of the returned reference may still be changed, but it's not possible to reassign the reference itself.)
 [2016-10-02 09:03 UTC] thekid@php.net
OK, I understand. This is originally a part of a unittest verifying the `finally` block is run; which it still is, just cannot change the return value anymore. Will need to rewrite it then.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Apr 29 14:01:29 2025 UTC