|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-07-14 21:31 UTC] dangerous dot ben at gmail dot com
Description:
------------
Using PHP 5.3 from svn.
When SPL's default autoloader is the only loader in the stack it triggers an error or throws an exception when it can't find a class. This means that you get an exception when calling class_exists() for a class that doesn't exist. This behaviour seems pointless anyway since PHP will trigger its own fatal error if the class still doesn't exist after attempting to autoload, so the attached patch simply removes it.
Test script:
---------------
spl_autoload_register();
class_exists('foo\bar');
Expected result:
----------------
No error
Actual result:
--------------
ben@arctor:~/src/php-5.3$ sapi/cli/php ~/code/cram/test.php
PHP Fatal error: Uncaught exception 'LogicException' with message 'Class foo\bar could not be loaded' in /home/ben/code/cram/test.php:4
Stack trace:
#0 [internal function]: spl_autoload('foo\bar')
#1 /home/ben/code/cram/test.php(4): class_exists('foo\bar')
#2 {main}
thrown in /home/ben/code/cram/test.php on line 4
Patchesspl_autoload.patch (last revision 2010-07-14 19:32 UTC by dangerous dot ben at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 05:00:01 2025 UTC |
I beg to differ. As you say, class_exists() attempts to autoload if there second param is true, but if autoloading fails it should simply return false as usual rather than throw an exception. Otherwise it is rather useless. The fact that this only occurs when there isn't another autoloader in the stack should make it clear that this is a bug. For example, the following code does not throw an exception: spl_autoload_register(); spl_autoload_register(function(){}); class_exists('foo\bar');PHP 5.3.8 (cli) (built: Aug 23 2011 11:50:20) Copyright (c) 1997-2011 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies <?php print_r(class_exists('asdfasdf'));//no error, just false spl_autoload_register(); print_r(class_exists('asdfasdf'));//LogicException. ?> <?php class_exists('asdfasdf');//no error, just false. spl_autoload_register('spl_autoload'); class_exists('asdfasdf');//no error, just false. ?> spl_autoload is inconsistent with itself.Even worse. The no arg register is inconsistent. <?php print_r(class_exists('asdfasdf'));//no error, just false. spl_autoload_register(); print_r(class_exists('asdfasdf'));//LogicException ?> <?php print_r(class_exists('asdfasdf'));//no error, just false. spl_autoload_register('spl_autoload'); spl_autoload_unregister('spl_autoload'); spl_autoload_register(); print_r(class_exists('asdfasdf'));//no error, just false. ?> If the autoload stack is empty by the time the no arg registered is called, how can it function differently than if the autoload stack has been emptied by the time the no arg register is called? This is nonsensical behavior, and manual for spl_autoload say nothing about it throwing. Let alone LogicException. I would have expected this to be a RuntimeException. As it depends on the specific environment it is run in as to whether or not spl_autoload will find something.