Adding __toString method to class defined below instantiation causes class to not be detected
Description
This is probably considered non-standard usage, but I stumbled over it by accident.
The following code:
<?php
$foo = new Test;
var_dump($foo);
class Test
{
public function __toString()
{
return '';
}
}
Resulted in this output:
PHP Fatal error: Uncaught Error: Class "Test" not found in /private/tmp/foo.php:3
Stack trace:
#0 {main}
thrown in /private/tmp/foo.php on line 3
Fatal error: Uncaught Error: Class "Test" not found in /private/tmp/foo.php:3
Stack trace:
#0 {main}
thrown in /private/tmp/foo.php on line 3
But I expected this output instead:
object(Test)#1 (0) {
}
If you remove the __toString method from the class, the code functions as expected.
PHP Version
8.1.1
Operating System
No response
I think this comes down to classes with __toString implicitly implementing Stringable. When classes have inheritance their definition becomes order-dependent. In this case that's not really true as Stringable is internal and will always be declared first. That said, the order dependence is a known issue and AFAIK a WONTFIX. I'm not sure it's worth adding a special case here.
Yeah, this is a known issue (see last paragraph of that section), but won't be fixed.
What's the reasoning for not fixing this bug?
Please read https://www.npopov.com/2021/10/20/Early-binding-in-PHP.html (and then quickly forget about that :)
Hi @cmb69, I understand the complexities around early binding and I'm not too surprised at the "won't fix" stance. That said, it does appear to be a BC breakage between PHP 7.x and PHP 8.x but as best I can tell, there is no reference to early binding in the BC documentation. I was therefore pretty confused when I ran into this issue whilst working on an advent of code challenge, when I swapped computers. It feels like something needs updating, should I create a ticket elsewhere for updating the documentation instead?
@pocallaghan, I'm not sure whether it makes sense to explain the complex early binding behavior in the PHP manual. Currently, there is a very much simplified explanation:
Unless autoloading is used, the classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.
The first sentence covers the example at hand.
That said, feel free to report a documentation bug or better a pull request for that repo.
It is worth adding that even using autoloading you can't use a class in a way that could make sense:
class A extends B {
}
class B {
public function __toString() : string {
return '';
}
}
Fatal error: Uncaught Error: Class "B" not found in line:1
That "autoloading" should probably be clarified to mean "autoloading with a single class per file".