php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Adding __toString method to class defined below instantiation causes class to not be detected

Open pocallaghan opened this issue 4 years ago • 8 comments

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

pocallaghan avatar Jan 03 '22 10:01 pocallaghan

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.

iluuu1994 avatar Jan 03 '22 10:01 iluuu1994

Yeah, this is a known issue (see last paragraph of that section), but won't be fixed.

cmb69 avatar Jan 03 '22 11:01 cmb69

What's the reasoning for not fixing this bug?

kenguest avatar Jan 03 '22 14:01 kenguest

Please read https://www.npopov.com/2021/10/20/Early-binding-in-PHP.html (and then quickly forget about that :)

cmb69 avatar Jan 03 '22 14:01 cmb69

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 avatar Jan 04 '22 08:01 pocallaghan

@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.

cmb69 avatar Jan 04 '22 10:01 cmb69

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

Geompse avatar Jan 04 '22 14:01 Geompse

That "autoloading" should probably be clarified to mean "autoloading with a single class per file".

cmb69 avatar Jan 04 '22 14:01 cmb69