2

I look at https://docs.joomla.org/J3.x:Developing_an_MVC_Component/Adding_a_model_to_the_site_part and found this codes in turorial

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld extends JViewLegacy
{
    /**
     * Display the Hello World view
     *
     * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
     *
     * @return  void
     */
    function display($tpl = null)
    {
        // Assign data to the view
        $this->msg = $this->get('Msg');

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

            return false;
        }

        // Display the view
        parent::display($tpl);
    }
}

look at $this->msg = $this->get('Msg'); , at first glance I thought the ('Msg') is parameter refer to member of JViewLegacy or value from elsewhere. I look at https://api.joomla.org/cms-2.5/classes/JViewLegacy.html#method_get it says

Returns a property of the object or the default value if the property is not set.

I found the explanation of this code it says

The HelloWorldViewHelloWorld class asks the model for data using the get method of the JViewLegacy. This get method converts the get('Msg') call into a getMsg() call on the model, which is the function which we have had to provide

from this explanation can I assume that if my function on class HelloWorldModelHelloWorld extends JModelItem named getMyMsg() then I can use $this->msg = $this->get('MyMsg')?. what if my function has no get as prefix for example my function called MyMsg() how do i use $this->msg = $this->get('?????') to get the value ?.

when it says

Returns a property of the object or the default value if the property is not set.

what is PROPERTY means ?. why get('Msg') suddenly becomes getMsg()?

1
  • Hi Dhany, please if you have some time go back to your earlier questions on which you got clear and helpful answers and if those are clear and acceptable for you too then click and accept those answers on your questions. Or if you need more clarification on the answers then ask for more help under the answer. Please, do not let your well answered questions hanging around. This is how this community works. Thank you for your contribution here!
    – Zollie
    Oct 9 '20 at 8:21
2

That's an error in 2.5 documentation. For some reason it shows description of parent JObject::get() method. In 2.5 code and in 3.0 documentation the description is this:

Method to get data from a registered model or a property of the view

https://api.joomla.org/cms-3/classes/Joomla.CMS.MVC.View.HtmlView.html#method_get

Which can still be confusing and not safe to use. For example, it could break if parent model class receives a get[property] method with [property] matching an actual property in your view.

It's not necessary to use this method anyways. Instead I recommend getting data from the model directly:

$model     = $this->getModel();
$this->msg = $model->getMsg();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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