I am thinking about using autoloading in a plugin. Outside of WordPress (as a test) it works as expected, but inside the plugin there are some problems.

The directory structure I used for testing:


You see, it's really simple and everything works fine.

The file index.php looks like this:

namespace Me;

spl_autoload_register(function ($class) {
    spl_autoload(strtolower(str_replace("\\", "/", $class)));

$greeter = new Greeter;

I declared my namespace and used the spl_autoload_register() function with some closure magic. Inside the anonymous function, I told PHP to autoload all necessary classes using spl_autoload(). The string replacement is needed to so I can map namespaces directly to directories.

From the manual of spl_autoload:

By default it checks all include paths to contain filenames built up by the lowercase class name appended by the filename extensions .inc and .php.

So to me, it looks like everything is okay and the fact that it works confirms this I think.

Now I've wrapped exactly the same code into a WordPress plugin by renaming index.php to greeter.php, putting it into a folder named greeter and putting the required comments to the top of the file.

Now, when I try to activate the plugin, a fatal error is thrown:

Fatal error: Class undefined: Me\\Greeter in /var/www/public/wp-content/plugins/greeter/greeter.php on line 20

I understand what this message means – PHP could not load the greeter class – but why does this happen?
On the same server it works outside of WordPress.

My only guess is that maybe the include path needs to be set before calling spl_autoload_register, like set_include_path(get_include_path().PATH_SEPARATOR.'/wp-content/plugins/greeter'); or something like that, but I am not sure. In my tests, that didn't change anything.

In the example above – why does the loader works outside of WordPress, but not inside?

I am running Apache on Ubuntu Trusty 14.04 LTS, using HHVM.

The greeter class is defined in me/greeter.php, like this:

namespace Me;

class Greeter {
    public function __construct() {
        echo __NAMESPACE__ . ' says Hello.';
  • Not much point in doing what you do if you create an object of the class all the time. Anyway, why do you think wordpress has anything to do with it, maybe it is just the php file being at some othe location.... what happens if you try to iclude it from outside of wirdpress? – Mark Kaplun Oct 14 '14 at 20:27
  • Where is the Greeter class defined? – s_ha_dum Oct 14 '14 at 23:01
  • @s_ha_dum in me/greeter.php – I've updated my question. – Sven Oct 15 '14 at 5:41
  • @MarkKaplun What exactly do you mean? The code above is just a simplified example, but somewhere greeter (or any other class) has to be instantiated? Anyway: I think WordPress has something to do with it, because the same structure works outside of it. All files are at the correct place. Probably because other things are loaded before the plugins load, or something like that. – Sven Oct 15 '14 at 5:48
  • 1
    @Sven, same structure is not good enough, does it work if you include the file from outside of wordpress?. I really don't see what wordpress has to do with class autoloading, it is a php feature not a wordpress one. If autoloading fails because of using the wrong actions then your simplified code is too simplified and doesn't show any of the important details – Mark Kaplun Oct 15 '14 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.