in WordPress

PSR-4 autoloading In the WordPress Plugins

Including the classes is one of the main concerns in the plugins for WordPress developers, Including me 🙂

If the WordPress were supported with Composer package manager, this issue could be solved but to fix this, I’ve created a sample plugin.

Structure

In the below you can see the folder structure and classes:

includes
    ├── Folder
    │   └── Test.php
    └── Folder2
        └── Test2.php

The following example is for Test.php

namespace Folder;

class Test
{
    /**
     * @return string
     */
    public static function getHelloWorld()
    {
        return 'Hello World!';
    }
}

And you can run above class in your code with below command:

echo Folder\Test::getHelloWorld();

Github Page!

Write a Comment

Comment