Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a plugin in which I do the following for autoloading classes:

function TFSLAutoload($class) {
    $classfile = dirname(__FILE__).'/includes/'.strtolower($class).'.php';
    include $classfile;
} // function TFSLAutoload
spl_autoload_register('TFSLAutoload');

In the plugin (classes) I can instantiate classes (e.g., use $myClassXYZ = new MyClassXYZ();) as well as use static functions (e.g., MyClassXYZ::my_static_func();) without including/requiring the file defining the very class. In short, everything is working as expected.

However, when having WP_DEBUG set to true, I do get the following warnings:

Warning: include(/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/wp_atom_server.php): failed to open stream: No such file or directory in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31 
Warning: include(): Failed opening '/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/wp_atom_server.php' for inclusion (include_path='.:') in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31 
Warning: include(/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/Recursive_ArrayAccess.php): failed to open stream: No such file or directory in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31 
Warning: include(): Failed opening '/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/Recursive_ArrayAccess.php' for inclusion (include_path='.:') in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31
Warning: include(/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/WP_Session.php): failed to open stream: No such file or directory in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31 
Warning: include(): Failed opening '/var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/includes/WP_Session.php' for inclusion (include_path='.:') in /var/www/vhosts/MY-DOMAIN.TLD/wp-content/plugins/tf-song-list/tf-song-list.php on line 31

I then changed my autoload function to check first if the file exists:

function TFSLAutoload($class) {
    $classfile = dirname(__FILE__).'/includes/'.strtolower($class).'.php';
    if (file_exists($classfile)) include $classfile;
    else echo "File »$classfile« does not exist.".PHP_EOL;
} // function TFSLAutoload
spl_autoload_register('TFSLAutoload');

Now, only my own plugin class files are loaded by the autoloader, as they are the only files loacted in the pre-defined folder.

However, if the three mentioned WordPress classes are still being 'sent' through my autolaoder class, are they even(tually) loaded correctly by WP? Or is my autoloader competing with the built-in autoloader of WP? How can I check that? Or: Am I doing the whole autoloading stuff the wrong way?

And why are these three classes sent to my autoloader? Are they the only ones to be load after the plugins are loaded?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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.

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.

share|improve this answer
    
Thanks for the great example. As for the theme: I wrote my own and there I don't use an autoloader, so I still don't know why these three files come into my autoloader. Anyway, I'll be giving your approach a try, while I'm pretty sure I'll be using the second way (creating an object) most of the time. Thanks again. –  t f Mar 6 '13 at 13:14
1  
All classes (class names) that are not available will be sent through all autoloaders until one loads the needed file. –  toscho Mar 6 '13 at 13:16
    
Okay, thanks for clarifying this. In other words, in my case, I'm perfectly fine with just ignoring these warnings and go on with what I'm using so far, right? Which doesn't mean I won't be taking a closer look at your approach. ;) –  t f Mar 6 '13 at 13:19
1  
Not really. When another plugin uses an autoloader too and calls a class name you are using too, your file will be included instead of the correct one. And never echo anything in an autoloader; return FALSE; when you didn’t find anything. –  toscho Mar 6 '13 at 13:22
add comment

If you have unique namespace for your classes it works. I use this scheme:

  1. Stuff all your classes in namespace, let's say myplugin (you may have further namespaces of course, like myplugin\otherstuff\...

  2. Create autoloader that loads classes only in this namespace, you can can use this, put it on top of your plugin file:

    define("MYPLUGIN_DIR", plugin_dir_path(__FILE__));
    
    // Notice strlen(...) calls should be written as number for speed
    spl_autoload_register(function ($className) {           
        // Make sure the class included is in this plugins namespace
        if (substr($className, 0, strlen("myplugin\\")) === "myplugin\\") {
            // Remove myplugin namespace from the className
            // Replace \ with / which works as directory separator for further namespaces
            $classNameShort = str_replace("\\", "/", substr($className, strlen("myplugin\\")));
            include_once MYPLUGIN_DIR . "classes/$classNameShort.php";
        }
    });
    

That's it, it works fine for multiple plugins if you have different namespace for each (and you should anyway).

share|improve this answer
add comment

You can use a plugin WordPress Autoloader now. It supports also your own autoload methods.

Please see WordPress Plugin page for more details.

Proper usage of PHP __autoload can dramatically reduce WP site load time.

Sample of your own __autoload function is problematic. You should have some sort validation, to make sure that you load your own classes only, to avoid problems.

Please note, that if you load classes, its reccomended to use require_once or i

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.