[wp-trac] [WordPress Trac] #13436: WordPress class_exists() conflicts with __autoload() and php_auto_prepend

WordPress Trac noreply at wordpress.org
Mon Jun 23 04:14:22 UTC 2014


#13436: WordPress class_exists() conflicts with __autoload() and php_auto_prepend
-------------------------------+----------------------
 Reporter:  galbus             |       Owner:
     Type:  enhancement        |      Status:  closed
 Priority:  normal             |   Milestone:
Component:  Bootstrap/Load     |     Version:  3.0
 Severity:  minor              |  Resolution:  wontfix
 Keywords:  needs-patch close  |     Focuses:
-------------------------------+----------------------
Changes (by rmccue):

 * status:  reopened => closed
 * resolution:   => wontfix
 * milestone:  4.0 =>


Comment:

 Replying to [comment:23 jcjcc]:
 > Try this
 >
 > {{{
 > spl_autoload_extensions('.php');
 > spl_autoload_register();
 >
 > var_dump(class_exists('Testing'));
 > var_dump(class_exists('\\Testing\\Foo'));
 > }}}

 Still no luck for me here:

 {{{
 $ wp shell
 wp> spl_autoload_extensions('.php');
 string(4) ".php"
 wp> spl_autoload_register();
 bool(true)
 wp> class_exists('Testing');
 bool(false)
 wp> class_exists('\\Testing\\Foo');
 bool(false)
 }}}

 Same result for `spl_autoload_register('spl_autoload')`. I suspect this
 depends on the PHP version as well as a bunch of other factors, but it
 seems inconsistent at best.

 > Let's leave it at that then after that.

 Closing it for now then. :)

 ---

 tl;dr for those seeing this ticket for the first time: we support
 PSR-4-compliant autoloaders, as well as most other autoloaders out of the
 box. `spl_autoload` throws an exception, which is against generally
 accepted behaviour in PHP userland, and hence can break.

 Those looking for a replacement can use the following, which should be a
 drop-in replacement for `spl_autoload` (although I've not tested it
 fully):

 {{{
 spl_autoload_register( function ( $class ) {
         $class = strtolower( $class );
         $class = str_replace( '\\', DIRECTORY_SEPARATOR, $class );

         $extensions = explode( ',', spl_autoload_extensions() );

         foreach ( $extensions as $extension ) {
                 $path = stream_resolve_include_path( $class . $extension
 );
                 if ( ! empty( $path ) ) {
                         include $path;
                         return;
                 }
         }
 } );
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/13436#comment:24>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list