Bug #79022 [Opn->Csd]: class_exists returns True for classes that are not ready to be used
| From: | laruence@php.net | Date: | Tue, 24 Dec 2019 06:25:23 +0000 |
| Subject: | Bug #79022 [Opn->Csd]: class_exists returns True for classes that are not ready to be used | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-224506@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79022&edit=1
ID: 79022
Updated by: laruence@php.net
Reported by: mech at themech dot net
Summary: class_exists returns True for classes that are not
ready to be used
-Status: Open
+Status: Closed
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 7.4.1
Block user comment: N
Private report: N
New Comment:
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=153c9cc346bae927394d3ce1378f77fb7c1e1be3
Log: Fixed bug #79022 (class_exists returns True for classes that are not ready to be used)
Previous Comments:
------------------------------------------------------------------------
[2019-12-23 22:24:13] mech at themech dot net
Description:
------------
I noticed this issue while debugging Koseven framework with PHP 7.4. It is related to class
autoloading.
Imagine there is an autoloader function registered with the following code inside:
if (class_exists('B', False)) {
new B();
}
If class B extends some other class A and autoloading function is called to load class A then
class_exists('B', false) returns true even if B is not "fully loaded" as its
parent class A is still to be loaded. So under PHP 7.4 the "new B()" is being executed and
throws "Fatal error: Uncaught Error: Class 'B' not found".
The same code works with PHP 7.3
Test script:
---------------
<?php
// classes/Foo.php: class Foo extends Bar {}
// classes/Bar.php: class Bar {}
function my_autoloader($class) {
if (class_exists('Foo', False)) {
new Foo();
}
if ($class == 'Foo' or $class == 'Bar') {
include "classes/$class.php";
}
}
spl_autoload_register('my_autoloader');
new Foo();
echo "end"
Expected result:
----------------
class_exists should not return true for the class that does not exist yet.atal
Actual result:
--------------
Fatal error, class not found.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79022&edit=1