1

Something along the lines of:

JLoader::Register('MyComHelperConfig', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/config.php');

class MyComController extends JControllerLegacy
{
    protected $default_view = MyComHelperConfig::getDefaultView();
}

I gather that this may involve writing method overrides over the base class. What would be the simplest way to do it?

3

You can't set a class property to a dynamic value, such as a method call, during declaration like you're attempting. See http://php.net/manual/en/language.oop5.properties.php

You would need to set it in your constructor.

JLoader::Register('MyComHelperConfig', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/config.php');

class MyComController extends JControllerLegacy
{
    protected $default_view = NULL;

    public function __construct($properties)
    {
        $this->default_view = MyComHelperConfig::getDefaultView();

        parent::__construct($properties);
    }
}

FYI, setting the property to NULL explicitly is not required. I set it out of habit. You could leave it out:

    protected $default_view; // same as "protected $default_view = NULL;"
  • Oh 'parent'. I was wondering what the equivalent for 'base' was in php. Much appreciated! PS: Apologies for not using legal code. I am aware that objects can't be given dynamic values in global space. I was too bent on portraying what result I needed. – RiA Nov 2 '17 at 2:48
  • 1
    :) no worries! Welcome to PHP. Happy coding. – Drew G Nov 2 '17 at 3:20

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.