[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-pure default arguments are mistakenly cached #9965

Closed
iluuu1994 opened this issue Nov 17, 2022 · 0 comments
Closed

Non-pure default arguments are mistakenly cached #9965

iluuu1994 opened this issue Nov 17, 2022 · 0 comments

Comments

@iluuu1994
Copy link
Member

Description

PHP compiles function default arguments to constant ASTs. The AST is evaluated when the function is called. When the result of the evaluation is a non-refcounted value, PHP assumes that the AST is pure (will always evaluate to the same value) and will then cache the value and skip re-evaluation in the next function call. This assumption is incorrect.

<?php

class Foo {
    public function __toString() {
        static $i = 0;
        return (string) $i++;
    }
}

function test(string $foo = new Foo() . '') {
    var_dump($foo);
}

test();
test();
test();

Resulted in this output:

string(1) "0"
string(1) "0"
string(1) "0"

But I expected this output instead:

string(1) "0"
string(1) "1"
string(1) "2"

Instead what should be done is evaluation of the AST should report whether the AST was pure or not. This currently mainly depends on whether new was used.

PHP Version

PHP 8.1

Operating System

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant