JLoader::register alternative

General questions relating to Joomla! 4.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
gba
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 117
Joined: Tue Jun 03, 2014 3:37 pm

JLoader::register alternative

Post by gba » Thu Feb 04, 2021 5:10 pm

Hello!

I am using following code to be able to extend the model class 'Manage' of the component com_installer:

Code: Select all

use Joomla\CMS\Version;
$JVer = (new Version)->getShortVersion();
if (version_compare($JVer, '4.0', '>=')) {  // For Joomla!4.0
	JLoader::register('ManageModel', JPATH_ADMINISTRATOR . '/components/com_installer/src/Model/ManageModel.php');
	class_alias('Joomla\Component\Installer\Administrator\Model\ManageModel', 'InstallerModelManage');
} else if (version_compare($JVer, '3.9', '>=')) {  // For Joomla!3.9
	JLoader::register('InstallerModelManage', JPATH_ADMINISTRATOR . '/components/com_installer/models/manage.php');
}

class MycompModelMymodel extends InstallerModelManage
{
	...
}
Is there also another, more advanced way to extend a class of another component, without using JLoader?

Thank you very much in advance for any useful hint!

Kind regards,
Gerald

gba
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 117
Joined: Tue Jun 03, 2014 3:37 pm

Re: JLoader::register alternative

Post by gba » Thu Oct 14, 2021 7:38 pm

Your help still is very much appreciated :)

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

Re: JLoader::register alternative

Post by SharkyKZ » Thu Oct 14, 2021 8:15 pm

On J4 you could create an alias for the namespaced class:

Code: Select all

if (version_compare(JVERSION, '4.0', '<'))
{
	JLoader::register('InstallerModelManage', JPATH_ADMINISTRATOR . '/components/com_installer/models/manage.php');
}
else
{
	JLoader::registerAlias('InstallerModelManage', 'Joomla\Component\Installer\Administrator\Model\ManageModel');
}

class MyInstaller extends InstallerModelManage
{

}

gba
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 117
Joined: Tue Jun 03, 2014 3:37 pm

Re: JLoader::register alternative

Post by gba » Thu Oct 14, 2021 8:41 pm

Thank you very much!


Locked

Return to “General Questions/New to Joomla! 4.x”