Bug #75943 [Opn->Nab]: autoload trying load existing class when it extends not existing class
| From: | requinix@php.net | Date: | Fri, 09 Feb 2018 08:42:18 +0000 |
| Subject: | Bug #75943 [Opn->Nab]: autoload trying load existing class when it extends not existing class | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213883@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75943&edit=1
ID: 75943
Updated by: requinix@php.net
Reported by: akademic at hub dot sknt dot ru
Summary: autoload trying load existing class when it extends
not existing class
-Status: Open
+Status: Not a bug
Type: Bug
Package: Class/Object related
Operating System: Ubuntu 16.04.3 LTS
PHP Version: 7.2.2
Block user comment: N
Private report: N
New Comment:
You've encountered an edge case when it comes to defining classes after they're used.
PHP runs files in two steps: a compilation phase where it tries to understand what's in a file,
and an execution phase where it actually runs the code. If a class can be fully understood during
the first phase then you can use it at any place in the file regardless of whether it comes before
or after the definition, but if not then class definition has to be "executed" during the
second phase.
Since class_two is not defined, class_one cannot be loaded during the first phase. In the second
phase, since test() executes before the class_one definition, PHP will not know the class exists yet
and thus will try to autoload it.
If you want PHP to try to autoload class_two then class_one has to be defined before test() tries to
use it.
https://3v4l.org/Ei3Ek
Previous Comments:
------------------------------------------------------------------------
[2018-02-09 08:22:52] akademic at hub dot sknt dot ru
Description:
------------
In example below I expect that autoload call back will try to load class_two.
But it try to load existing class_one.
If you comment out 'extends class_two', autoload callback will not start.
Test script:
---------------
spl_autoload_register(function($class) {
var_dump('autoload '.$class);
});
test();
function test() {
$m = new class_one();
}
class class_one extends class_two {
function __construct() {
var_dump('I am '.__CLASS__);
}
}
Expected result:
----------------
string(18) "autoload class_two"
Actual result:
--------------
string(18) "autoload class_one"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75943&edit=1