【问题标题】:Accessing PHP Class Method in Joomla在 Joomla 中访问 PHP 类方法
【发布时间】:2016-11-24 09:15:46
【问题描述】:

Joomla 中有一个示例 Hello Word 组件。

在模型中:

class HellowWorldModelHelloWorld extends JModel
{
Protected $message;

Public function getMsg()
{
if (!isset($this->message))
{
$this->message = 'Hello World';
}
return $this->message;
}
}

在视图中:

class HelloWorldViewHelloWorld extends JViewLegacy
{

    function display($tpl = null)
    {
        $this->msg = $this->get('Msg');

        if (count($errors=$this->get('Errors')))
        {
            JLog::add(implode('<br/>',$errors),JLog::WARNING, 'jerror');

            return false;
        }

        parent::display($tpl);
    }
}

现在我的问题是它是如何工作的,据我所知,我无法理解下面的代码

$this->msg = $this->get('Msg');

第一个$this-&gt;msg - msg 属性未在类或父类中的任何位置定义。那么如何通过直接访问$this-&gt;msg来定义或设置msg

第二个模型中的方法定义为function getMsg()。所以我认为它应该可以作为$this-getMsg() 访问(但它不起作用)但它正在作为$this-get('Msg') 访问并且它工作正常。

详细的示例答案将帮助我正确理解我所缺少的。

【问题讨论】:

    标签: php class methods joomla


    【解决方案1】:

    不确定我能做到多详细,但执行$this-&gt;msg = $this-&gt;get('Msg) 相当于执行位于模型中的function getMsg()

    因此,在您的视图中,$this-&gt;msg = $this-&gt;get('Msg') 将使用模型中的 function getMsg() 返回的任何内容填充 变量 $this-&gt;msg

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 2015-03-31
      相关资源
      最近更新 更多