3

I have created a component and a plugin in joomla 2.5 and there is a Helper file in the component which will have many useful functions and I plan to call one of its function which then calls another function in the helper by this code:

$this->getinformation();

and it gives me this error :

Fatal error: Call to undefined method

My questions are:

  • Why can't I call a function in a helper in Joomla?
  • How can I call a function inside helper class?
  • Is there any class structure which i missed in this code?
  • First off in order to use $this your helper file needs to be a class. Is it a a class or just a collection of functions. What does your helper file look like? – Cleanshooter Nov 20 '12 at 16:05

2 Answers2

5

Helper files are typically called statically and not using $this

First create your helper file and add methods like this:

Class myHelper {

    //This method can call other methods
    public static function myMethod($var) {

        //Call other method inside this class like this:
        self::myOtherMethod($var);

    }

    //This method is called by myMethod()
    public static function myOtherMethod($var) {

        //Put some code here

    }

}

Simply include the helper file like this in the documents that you would to use it:

require_once JPATH_COMPONENT.'/helpers/my_helper.php';

Then use it like this:

myHelper::myMethod($var);

or

myHelper::myOtherMethod($var);
Søren Beck Jensen
  • 1,676
  • 1
  • 12
  • 22
0

You have to include the helper file and call the function using the classname

Add the following line in the plugin or component:

jimport( 'joomla.filesystem.folder' );
require_once JPATH_ROOT . '/components/com_xxxx/helper.php';

classname::functionname();

OR

If you are working on the same helper file means then call like this

classname::functionname();
Mohammed Nagoor
  • 884
  • 2
  • 12
  • 25