php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81605 Regression in class loading order when __toString() is implemented
Submitted: 2021-11-10 15:55 UTC Modified: 2021-11-10 16:01 UTC
From: marco at tremani dot nl Assigned:
Status: Duplicate Package: Unknown/Other Function
PHP Version: 8.0.12 OS: Windows/Unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
25 + 27 = ?
Subscribe to this entry?

 
 [2021-11-10 15:55 UTC] marco at tremani dot nl
Description:
------------
I'm not sure if this can be called a bug, unexpected or undefined behavior - but behavior for the test case below changed in PHP 8.0.

The file below "a.php" implements this pattern: 1. include another file "b.php". 2. declare a class. If that class contains a method "__toString()", the class won't be available yet in b.php. I don't know if this is the direct cause or merely a symptom of compiler behavior.

The example is a bit contrived but it demonstrates the changed behavior clearly. It was encountered and extracted when upgrading a legacy php library (nusoap).

Test script:
---------------
File: a.php

```
    <?php

    include('b.php');

    class a
    {
        public function __toString()
        {
            return '';
        }
    }
```

File: b.php

```
    <?php var_dump(class_exists('a'));
```

Expected result:
----------------
> php a.php
bool(true)

Actual result:
--------------
On PHP 7.4:
bool(true)

On PHP 8.0:
bool(false)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-10 16:01 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2021-11-10 16:01 UTC] nikic@php.net
Duplicate of bug #79350.

https://www.npopov.com/2021/10/20/Early-binding-in-PHP.html may be helpful as background ;)
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu May 19 12:05:44 2022 UTC