I have an issue after upgrade from PHP 7.3 to 8.1.1
There is lot to be done of course, but this is kind of weird.
This example is not working for me with error Fatal error:
Uncaught Error: Class "TestC" not found in
C:xampp81htdocshelpdesk811test81index.php:2 Stack trace: #0
{main} thrown in C:xampp81htdocshelpdesk811test81index.php on
line 2
<?php
$a = new TestC;
echo $a->a;
class TestC
{
public $a = "a_value";
public $b;
public function __toString()
{
return "string";
}
}
If I define class and create instance later, it works, but I was not able to find any documentation for this behavior.It’s the same with static method (public static function foo(){echo "bar";}).
I tried 3v4l.org sandbox and it works in versions >5.0 && <8.0
2
Answers
I was digging deeper and it looks like it's really the BUG. https://bugs.php.net/bug.php?id=79350
You can use a class before defining it, but only if early binding is allowed. Early binding does not work if some dependencies are not available yet, if the class uses traits or if it implements an interface. Since PHP 8 there is a new
Stringable
interface and every class that defines the__toString()
function implicitly implements that interface, thus preventing early binding.Nikita Popov (nikic) wrote an article about Early binding in PHP in which he mentions that this behavior is indeed not well documented.