[php-src] Issue #12320: Class not found when defined after use, if it contains a __toString method
| From: | tobia | Date: | Fri, 29 Sep 2023 08:42:16 +0000 |
| Subject: | [php-src] Issue #12320: Class not found when defined after use, if it contains a __toString method | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245455@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12320
Author: tobia
### Description
The following code:
```php
<?php
new A();
echo "OK";
class A {
public function __toString() {
return "A";
}
}
```
Resulted in this output:
```
Uncaught Error: Class "A" not found
```
But I expected this output instead:
```
OK
```
Here is a simple way to test multiple versions in Bash:
for ver in 7.4.33 8.0.0alpha1 8.2.10 8.2.11RC1 8.3.0RC2; do echo $ver:; docker run --rm
php:$ver -r 'new A; echo "OK\n"; class A { function __toString() { return
"A"; } }'; echo; done
Result:
```
7.4.33:
OK
8.0.0alpha1:
Fatal error: Uncaught Error: Class 'A' not found in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
8.2.10:
Fatal error: Uncaught Error: Class "A" not found in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
8.2.11RC1:
Fatal error: Uncaught Error: Class "A" not found in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
8.3.0RC2:
Fatal error: Uncaught Error: Class "A" not found in Command line code:1
Stack trace:
#0 {main}
thrown in Command line code on line 1
```
As you can see, this bug was introduced in the 8.x branch, because all 8.x versions are affected,
from the first alpha to the latest RCs, including all stable versions I tried. 7.x and earlier are
unaffected.
I did not test if other magic methods have the same effect.
### PHP Version
8.x
### Operating System
_No response_