Page 1 of 1

JModelList or \Joomla\CMS\MVC\Model\ListModel

Posted: Tue Apr 09, 2019 10:16 am
by Soren Jensen
Should I be writing this:

1:

Code: Select all

class ExampleModelExample extends JModelList
and

Code: Select all

$app = JFactory::getApplication();
or

2:

Code: Select all

class ExampleModelExample extends \Joomla\CMS\MVC\Model\ListModel
and

Code: Select all

$app = \Joomla\CMS\Factory::getApplication();
Option 2 is preferred and makes the IDE much happier, but I do not see a "deprecated" about this, and if it is indeed option 2 why is the core not using this?

Also is there a list of changes I should be using?

Will the documentation eventually be updated?

Finally, does the

Code: Select all

\Joomla
method work on all versions of PHP?

Re: JModelList or \Joomla\CMS\MVC\Model\ListModel

Posted: Tue Apr 09, 2019 11:15 am
by SharkyKZ
For new extensions use namespaced classes (option 2). Class aliases (option 1) are deprecated and will be removed in future Joomla versions (most in 5.0 and a few in 4.0).

Core will use namespaced classes in 4.0. But coding standards manual calls for classes to be imported with "use" keyword. So in Joomla core it looks like this:

Code: Select all

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;

class ExampleModelExample extends ListModel
{
	public function whatever()
	{
		$app = Factory::getApplication();
	}
}
Namespacing in PHP is available since PHP 5.3 and Joomla libraries have been namespaced since Joomla 3.8.

Re: JModelList or \Joomla\CMS\MVC\Model\ListModel

Posted: Tue Apr 09, 2019 1:03 pm
by Soren Jensen
Thanks for that. Does this mean that I should not write Joomla\CMS\Factory::getApplication();

Would it be wrong if I did? Would/will it break something?

Re: JModelList or \Joomla\CMS\MVC\Model\ListModel

Posted: Wed Apr 10, 2019 6:06 am
by SharkyKZ
It's a matter of preference. Using imports means not having to prefix the namespace later on in the code. This makes the code shorter and more readable. On the other hand, you have to avoid class collisions by using aliases.

Re: JModelList or \Joomla\CMS\MVC\Model\ListModel

Posted: Wed Apr 10, 2019 7:30 am
by Soren Jensen
Thanks for your help! Much appreciated.