- Notifications
You must be signed in to change notification settings - Fork8k
Closed
Labels
Description
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.
<?phpclass Foo {publicfunction__toString() {static$i =0;return (string)$i++; }}functiontest(string$foo =newFoo() .'') {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 whethernew
was used.
PHP Version
PHP 8.1
Operating System
No response