简体   繁体   中英

Accessing PHP Class Method in Joomla

There is an example Hello Word component in Joomla.

In Models:

class HellowWorldModelHelloWorld extends JModel
{
Protected $message;

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

in Views:

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);
    }
}

Now my question is how this works, as far as I have learned, I can't make it out the below code

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

First $this->msg - msg property is not defined anywhere in the class or in the parent class. So how msg is get defined or set by directly accessing $this->msg

Second the method in the model is defined as function getMsg() . So I think it should be accessible as $this-getMsg() (but it doesn't work) but it is being accessed as $this-get('Msg') and it is working fine.

A detailed answer with example will help me properly understand what I am missing.

Not sure how detailed I can make this, but executing $this->msg = $this->get('Msg ) amounts to executing function getMsg() that is located in the model.

So, in your View, $this->msg = $this->get('Msg') will populate the variable $this->msg with whatever is returned by the function getMsg() in the Model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM