|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2017-04-04 20:31 UTC] lightnb at bellsouth dot net
Description:
------------
Attempting to autoload a class file with a syntax error causes PHP to attempt to use the next autoloader rather than throwing the syntax error.
Test script:
---------------
// Use built in autoloader for 99% of loads
spl_autoload_register();
// If that fails, see if it's a rare special case
spl_autoload_register('AutoLoadFallback');
function AutoLoadFallback($ClassName){
// ... try stuff...
throw new Exception('Could not autoload the class definition for "'.$ClassName.'"');
}
// Try to load a new widget object
$oWidget = new Widget();
Expected result:
----------------
In this case, assume that the Widget object has a file that the default autoloader can find, but the file contains a syntax error.
The expected result would be a syntax error thrown for the faulty file.
But what actually happens is, when an autoload file is found by the default autoload but a syntax error exists in it, PHP calls the next autoloader in the chain AutoLoadFallback, which of course throws an exception because it only handles cases that the default autoloader can't.
The result is getting an exception thrown by the AutoLoadFallback function, rather than a parse error being thrown when the default autoloader tries to load the class. This blocks me from seeing the real error and line number.
If I comment out the spl_autoload_register('AutoLoadFallback'); line, it throws a parse error per normal.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 04:00:02 2025 UTC |
@lightnb: I had tried with the standard autoloader first. Same thing happens. The issue is in the autoloading process, not spl_autoload() itself. Fatal error: Uncaught ParseError: syntax error, unexpected end of file, expecting function (T_FUNCTION) in /root/php/PHP-7.0.17/widget.php:4 Stack trace: #0 [internal function]: spl_autoload('Widget') #1 /root/php/PHP-7.0.17/bug.php(15): spl_autoload_call('Widget') #2 {main} Next Exception: Could not autoload the class definition for "Widget" in /root/php/PHP-7.0.17/bug.php:10 Stack trace: #0 [internal function]: AutoLoadFallback('Widget') #1 /root/php/PHP-7.0.16/bug.php(15): spl_autoload_call('Widget') #2 {main} thrown in /root/php/PHP-7.0.17/bug.php on line 10 Bundling the exceptions is a bit unusual for PHP - normally it would be one error at a time - but I don't think there's any harm in it being there. An exception still gets thrown if one occurs, with the difference being that autoloading continues and therefore the class could still be loaded before the exception goes up the call stack. https://3v4l.org/IlUiA