Skip to content

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

@pocallaghan

Description

@pocallaghan

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions