1

Is there a conventional way of making a helper file to a Joomla Plugin? Like class names (helper or plgNameHelper) and the way of calling it?

class Helper
{
    public static function test() 
    {
    // some code
    }

    public static function anotherTest() 
    {
    // some code
    }
}
1
  • Sorry, this probably a very noob question, but what is a (Joomla) "helper file" in this context? At first I thought it was some way to document your plugin for other developers, but judging by the answer I guess not!?
    – MrWhite
    Mar 26, 2015 at 10:46

1 Answer 1

2

Yes, this is possible. Here is a small example:

myplugin.php

class Myplugin
{
    public function doSomething()
    {
        require_once __DIR__ . '/helper.php';
        $newuser = new User;

        return $newuser->getUsername();
    }
}

helper.php

class User
{
    public function getUsername()
    {
        // perform some magic
    }
}

Put both the PHP files in the same directory and simply call it using:

require_once __DIR__ . '/helper.php';

Hope this helps

8
  • It helped a lot. The class name in helper file should respect any kind of Joomla terminology?
    – n.h.
    Mar 24, 2015 at 21:49
  • 2
    Of course, I would refrain from using Joomla class names. It would be best to use your plugin name as a prefix, for example: MypluginUser
    – Lodder
    Mar 24, 2015 at 23:07
  • @Lodder could you use JLoader::import instead of require once? Are there any differences/benefits?
    – Eoin
    Apr 30, 2018 at 15:43
  • 1
    @Eoin - \JLoader::register('MyCustomClass', __DIR__ . '/helper.php');
    – Lodder
    Apr 30, 2018 at 18:28
  • 1
    Please check the docs for JLoader. It will explain what it does
    – Lodder
    Apr 30, 2018 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.