WordPress Plugin development: How to use autoloading?

Luís Nóbrega
3 min readSep 1, 2018

--

Hand with WordPress Logo. Credits.

When we are developing in PHP one of the most important issues is how to manage the inclusion of classes to be instantiated.

While autoloading may be the best solution, there are a few issues to consider, such as the PHP version that we have, and the PHP version that supports namespaces.

In any case, I explain in this article how to use autoloading with PSR-4.

What is autoloading?

Is a mechanism that automatically loads classes that have not yet been instantiated, including the files that contain them.

So we do not need to be worried about requiring the classes we need, one by one, boring, manual, and fallible process.

Just see a very used boilerplate plugin from github, to realize the idea.

require_once plugin_dir_path (dirname (__FILE__)). ‘includes / class-plugin-name-loader.php’;
require_once plugin_dir_path (dirname (__FILE__)). ‘includes / class-plugin-name-i18n.php’;
require_once plugin_dir_path (dirname (__FILE__)). ‘admin / class-plugin-name-admin.php’;

But to have an autoloader we must also set a standard for the file naming, something we will see later with the PSR-4.

Autoloading drawbacks

The issue is compatibility. Although the spl_autoload_register built in function is available in PHP starting with version 5.1.2, the namespaces are not.

The namespaces appeared only in version 5.3 of PHP.

So if we want to use autoloading, our plugin may not be able to be used by users that have older versions.

But, we can also have autoloading without namespaces. For example, this autoloader has defined a rule for class names, WordPress style.

So, if we instantiate a class of name ‘Main’ the autoloader loads a file in /class-main.php.

Implement an autoloader

There are several possibilities, but if we define that the minimum version that PHP should have for our plugin to work is 5.4, then one of the good option is to use the official example of the PSR-4.

If we do not want to make this restriction, the Rarst autoloader mentioned above is a good alternative.

But what is PSR-4 anyway?

The PSR-4 is a standard defined by the PHP Framework Interop Group, a group of people from the PHP community, which has representatives from the largest PHP frameworks and CMS

They set standards for the most common PHP development practices, and among them are two standards for autoloaders: PSR-0 and more recently PSR-4.

They are very similar standards, but they have two major differences. First, the PSR-4 requires namespaces, and second, the home directory for a project, following the PSR-4, does not need to be the base namespace.

By Closure or Class

In a WordPress plugin I developed recently, I opted for the closure version. It was enough to have a file with autoload, which I had to manually include in the main plugin file.

However if we use the class version we will have the advantage of being able to register several namespaces.

This may be advantageous if we want, for example, to have one namespace for the code, and another for the tests.

What do you usually use?

If you have already developed WordPress plugins, how do you usually solve this problem?

Do you use autoloading, or do you include the files you need manually? If so, why do not they still use autoloading?

--

--

Luís Nóbrega

Web Developer at 25friday. Likes to travel, write and being a father.