Joomla 2.5无法在帮助文件中使用$ this

时间:2012-11-20 11:31:51

标签: joomla joomla2.5 joomla-extensions

我在joomla 2.5中创建了一个组件和一个插件,组件中有一个Helper文件,它有许多有用的函数,我打算调用它的一个函数,然后在helper中调用另一个函数通过这段代码:

$this->getinformation();

它给了我这个错误:

Fatal error: Call to undefined method

我的问题是:

  • 为什么我不能在Joomla的助手中调用函数?
  • 如何调用辅助类中的函数?
  • 我在这段代码中遗漏了哪些课程结构?

2 个答案:

答案 0 :(得分:5)

辅助文件通常是静态调用而不是使用$ 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

    }

}

只需在您要使用的文档中包含这样的帮助文件:

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

然后像这样使用它:

myHelper::myMethod($var);

myHelper::myOtherMethod($var);

答案 1 :(得分:0)

您必须包含帮助文件并使用classname

调用该函数

在插件或组件中添加以下行:

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

classname::functionname();

OR

如果您正在使用相同的帮助文件,则请按此方式调用

classname::functionname();