Stop the prefixing

| in


The WordPress codex still lists the following advice to avoid naming collision on your plugin:

A naming collision happens when your plugin is using the same name for a variable, function or a class as another plugin.

Luckily, you can avoid naming collisions by using the methods below.

WordPress Codex

This might have been helpful advice in 2012, pre-namespace PHP.

Not anymore.

Now, WordPress requires PHP5.6+ which means namespace support is always ensured.

If you are currently doing this:

function my_plugin_create_customer() {

}

class MyPluginLogger{
    
    public function log(string $message)
    {
        // 
    }
    
}

const MY_PLUGIN_LOG_ACTIONS = true;
my_plugin_create_customer();

$logger = new MyPluginLogger();

if(MY_PLUGIN_LOG_ACTIONS) {
    $logger->log('something');
}

you can safely switch it to:

namespace MyPlugin;

function create_customer() {
 //
}

class Logger {
    
    public function log(string $message)
    {
        //
    }
    
}

const LOG_ACTIONS = true;

*You should not define functions, classes, and constants in the same file.

use MyPlugin\Logger;

use function MyPlugin\create_customer;

use const MyPlugin\LOG_ACTIONS;

create_customer();

$logger = new Logger();

if(LOG_ACTIONS) {
    $logger->log('something');
}

You can stop yourself from writing all this boilerplate code and as an extra benefit you can now open any file and quickly get an overview of all the classes, functions, and constants used.

Happy coding.