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

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
User avatar
Soren Jensen
Joomla! Explorer
Joomla! Explorer
Posts: 290
Joined: Fri Nov 11, 2005 8:53 am
Location: Granada, Spain
Contact:

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

Post by Soren Jensen » Tue Apr 09, 2019 10:16 am

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?
Soren Beck Jensenhttp://www.component-creator.com/ - Build Joomla Components fast and easy

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1626
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

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

Post by SharkyKZ » Tue Apr 09, 2019 11:15 am

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.

User avatar
Soren Jensen
Joomla! Explorer
Joomla! Explorer
Posts: 290
Joined: Fri Nov 11, 2005 8:53 am
Location: Granada, Spain
Contact:

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

Post by Soren Jensen » Tue Apr 09, 2019 1:03 pm

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?
Soren Beck Jensenhttp://www.component-creator.com/ - Build Joomla Components fast and easy

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1626
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

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

Post by SharkyKZ » Wed Apr 10, 2019 6:06 am

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.

User avatar
Soren Jensen
Joomla! Explorer
Joomla! Explorer
Posts: 290
Joined: Fri Nov 11, 2005 8:53 am
Location: Granada, Spain
Contact:

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

Post by Soren Jensen » Wed Apr 10, 2019 7:30 am

Thanks for your help! Much appreciated.
Soren Beck Jensenhttp://www.component-creator.com/ - Build Joomla Components fast and easy


Post Reply

Return to “Joomla! 3.x Coding”