Problems with autoloading classes via sp_autoload_register / maybe interfering WP-specific autoloader

There is no built-in autoloader in WordPress. But other plugins or a theme might have registered an autoloader, and depending on the order of execution these are called earlier or later than yours.


Update 21. 08. 2014 This answer is no longer what I would recommend. Do use an autoloader if you need one.


The only way to make sure the called class is in your code’s responsibility is checking with file_exists() and testing the class name.

file_exists() is slow and its result is cached by the file system, so you cannot rely on it anyway.

I recommend not to use an autoloader. Use a plugin specific class loader instead.

Example:

/**
 * Load a class from /php/ directory.
 *
 * There is no file_exists() check to improve performance.
 *
 * @param  string  $class         Class name
 * @param  boolean $create_object Return an object or TRUE
 * @return bool|$class
 */
function t5_es_load_class( $class, $create_object = FALSE )
{
    // create the path base just once
    static $base = FALSE;

    ! $base && $base = plugin_dir_path( __FILE__ ) . 'php';
    ! class_exists( $class ) && require "$base/class.$class.php";

    return $create_object ? new $class : TRUE;
}

Now you can use this function either to just include the class …

t5_es_load_class( 'T5_Metabox_Base' );
$metabox = new T5_Metabox_Base( 'preview' );

… or create an object …

$data               = t5_es_load_class( 'T5_Property_List', TRUE );
$data->plugin_url   = plugin_dir_url( __FILE__ );
$data->plugin_path  = plugin_dir_path( __FILE__ );

Faster, more reliable, more flexible.

Leave a Comment