[wp-trac] [WordPress Trac] #30203: Consider a Simple WordPress-specific Autoloader

WordPress Trac noreply at wordpress.org
Thu Oct 30 22:38:55 UTC 2014


#30203: Consider a Simple WordPress-specific Autoloader
----------------------------+-----------------------------
 Reporter:  MikeSchinkel    |      Owner:
     Type:  enhancement     |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Bootstrap/Load  |    Version:  4.0
 Severity:  normal          |   Keywords:
  Focuses:  performance     |
----------------------------+-----------------------------
 I'm familiar with #21300 and this is not the same.

 I'd like to propose we consider adding the simplest possible autoloader to
 WordPress and change WordPress core to use the autoloader for all of it's
 classes.

 In my testing I've found that it can improve memory usage by about 2% on a
 non-plugin site, which is not much relatively speaking but on
 WordPress.com that 2% could add up to an extra site for every 50 sites
 that can currently run on a server.  But as I think WordPress.com hosts
 around 30 millions sites, that's enough memory for around 600,000 more
 sites (unless my calculations aren't taking something into consideration
 they should, which is quite possible. But still.)

 Here's what such a propose autoloader might look like:

 {{{
 global $wp_autoload_classes;
 $wp_autoload_classes = array(
         'WP_List_Table' => 'wp-admin/includes/class-wp-list-table.php',
         'WP_Post_List_Table' => 'wp-admin/includes/class-wp-post-list-
 table.php',
         // ...And all the rest of classes in core.
 );
 spl_autoload_register( 'wp_autoloader' );

 function wp_autoloader( $class_name ) {
     global $wp_autoload_classes;
     if ( isset( $wp_autoload_classes[ $class_name ] ) ) {
         $filepath = $wp_autoload_classes[ $class_name ];
         if ( '/' == $filepath[0] ) { // @todo Make Windows compatible
             require_once( $filepath );
         } else {
             require_once( dirname( __FILE__ ) . "/{$filepath}" );
         }
     }
 }
 function register_autoload_class( $class_name, $class_filepath ) {
     global $wp_autoload_classes;
     $wp_autoload_classes[ $class_name ] = $class_filepath;
 }
 }}}

 Then plugins could use the autoloader like so:

 {{{
 register_autoload_class( 'Jetpack_Admin', dirname( __FILE__ ) . '/class
 .jetpack-admin.php' );
 register_autoload_class( 'Jetpack_Client', dirname( __FILE__ ) . '/class
 .jetpack-client.php' );
 register_autoload_class( 'Jetpack_Data', dirname( __FILE__ ) . '/class
 .jetpack-data.php' );
 //... and so on...
 }}}

 If this gets the green-light from the core developers I'll be happy to do
 the work to create a patch for review.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30203>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list