Edit report at http://bugs.php.net/bug.php?id=52339&edit=1

 ID:                 52339
 Comment by:         james at nearlysensical dot com
 Reported by:        dangerous dot ben at gmail dot com
 Summary:            SPL autoloader breaks class_exists()
 Status:             Bogus
 Type:               Bug
 Package:            SPL related
 Operating System:   any (debian)
 PHP Version:        5.3.3RC2
 Block user comment: N

 New Comment:

I 100% agree with dangerous dot ben. class_exists should return false if
the class 

can't be autoloaded. Allowing it to do so would make it much easier to
use an 

autoloader in contexts where you're interacting with an existing
codebase that may 

not be so spl_autoload friendly. Bump.


Previous Comments:
------------------------------------------------------------------------
[2010-07-15 08:18:01] dangerous dot ben at gmail dot com

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');

------------------------------------------------------------------------
[2010-07-15 05:11:04] ka...@php.net

You are calling class_exists() with just a class name, which leaves the
second parameter ($autoload) set to true, which then invokes SPL and
throws the exception, so no bug here

------------------------------------------------------------------------
[2010-07-14 21:54:08] dangerous dot ben at gmail dot com

On further investigation, it appears that the error is meant to happen
only if spl_autoload is called directly, and not via spl_autoload_call. 
Unfortunately when spl_autoload is the only autoloader in the stack it
gets called directly and spl_autoload_call doesn't get a look in.

------------------------------------------------------------------------
[2010-07-14 21:31:46] 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:
--------------
b...@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




------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52339&edit=1

Reply via email to