How can I use a helper file in a system plugin?

32 Views Asked by At

I'm try to add and use an helper file to my system plugin for Joomla 4. Added namespace MyCompany\Plugin\System\MyPlugin to xml manifest.

This is the helper.php

namespace MyCompany\Plugin\System\MyPlugin\Helper;

defined('_JEXEC') || die;

class PlgMyPluginHelper
{
    public static function test()
    {
        return 'HELLO!';
    }
}

This is the myplugin.php

defined('_JEXEC') || die;

use Joomla\CMS\Helper\PluginHelper;
use MyCompany\Plugin\System\MyPlugin\Helper\PlgMyPluginHelper;

class PlgSystemMyPlugin extends CMSPlugin
{
    protected $app
    public function onContentPrepareForm(Form $form, $data)
    {
        $this->app->enqueueMessage(PlgMyPluginHelper::test(), 'notice');
    }
}

myplugin.php and helper.php are in the same directory. When I call PlgMyPluginHelper::test() appears Class "MyCompany\Plugin\System\MyPlugin\Helper\PlgMyPluginHelper" not found

If I add require_once dirname(__FILE__) . 'helper.php'; it works.

What's wrong?

EDIT: Solved reading the PSR-4: Autoloader specification.

0

There are 0 best solutions below