php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #75765 Fatal error instead of Error exception when base class is not found
Submitted: 2018-01-05 12:41 UTC Modified: 2018-01-06 20:07 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: contact at joycebabu dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 7.2.1 OS:
Private report: No CVE-ID: None
 [2018-01-05 12:41 UTC] contact at joycebabu dot com
Description:
------------
When the base class of a dynamically loaded class is not found, PHP raises a fatal error instead of throwing an exception.



Test script:
---------------
<?php
spl_autoload_register(function ($class_name) {
	if (file_exists(__DIR__ . "/{$class_name}.php")) {
		include __DIR__ . "/{$class_name}.php";
	}
});

// Following code throws an exception since CDE is not found
try {
	$a = new CDE();
} catch (\Throwable $e) {
	echo 'CAUGHT CDE', PHP_EOL;
}

/*
 * Create ABC.php with an empty class ABC extending base class CDE
 * <?php class ABC extends CDE {}
 */
// Following code raises a fatal error since CDE (base class of ABC) is not found
try {
	$a = new ABC();
} catch (\Throwable $e) {
	echo 'CAUGHT CDE', PHP_EOL;
}


Expected result:
----------------
CAUGHT CDE
CAUGHT CDE

Actual result:
--------------
CAUGHT CDE
PHP Fatal error:  Class 'CDE' not found in /path/to/file/ABC.php on line 3

Fatal error: Class 'CDE' not found in /path/to/file/ABC.php on line 3

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-06 19:59 UTC] timok at ya dot ru
Issue is not related with autoloading. Same result occurs always when extending some undefined class regardless autoload settings: https://3v4l.org/Drv16
Also the "implements" statement behaves as well. The problem reproduced at least from 7.0 version (when most of fatal errors was replaced by Error exceptions).
 [2018-01-06 20:07 UTC] nikic@php.net
-Type: Bug +Type: Feature/Change Request -Package: Dynamic loading +Package: Scripting Engine problem
 [2018-01-06 20:07 UTC] nikic@php.net
Supporting this would require larger changes to class binding, otherwise throwing would leave behind a partially-bound class.
 [2018-01-12 17:44 UTC] nikic@php.net
Automatic comment on behalf of timok@ya.ru
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a484b9a5359bd8252234c3965a6fb7f1a1a98b4a
Log: Fix #75765 Exception on extend of undefined class
 [2018-01-12 17:44 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Dec 12 07:01:25 2018 UTC