Skip to content

Returning by ref + finally allow circumventing return type checks #18736

Closed

Description

@iluuu1994

Description

The following code:

https://3v4l.org/BRBW9

<?php

function &test(): int {
    $x = 0;
    try {
        return $x;
    } finally {
        $x = 'test';
    }
}

$x = &test();
var_dump($x);

Resulted in this output:

string(4) "test"

But I expected this output instead:

Fatal error: Uncaught TypeError: test(): Return value must be of type int, string returned

PHP Version

PHP 7.0.2+

Operating System

No response

Activity

  1. DanielEScherzer commented on Jun 4, 2025

    @DanielEScherzer
    Member

    Naive guess that this might be from 778e5f3, which moved the finally handling to after the return type check

  2. added a commit that references this issue on Jul 31, 2025
    d0fad34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions