JLoader::registerNamespace Not Working? Topic is solved

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
sazbaztaz
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Thu Mar 18, 2010 2:20 pm

JLoader::registerNamespace Not Working?

Post by sazbaztaz » Tue Apr 09, 2019 7:38 am

I'm trying to create my own library to call classes GLOBALLY from anywhere in applications like the JFactory class for example. I've spent hours browsing and trying to figure out what am I doing wrong and it's doing my nut in.

Heres my code...

The system plugin I've created and it works because uncommenting the exit(); line in the constructor leaves pages with a blank page...

Code: Select all

defined('_JEXEC') or die;
class PlgSystemFoo extends JPlugin
{
	public function __construct()
	{
		// exit();
		JLoader::registerNamespace('Foo', JPATH_LIBRARIES . '/foo');
	}
	
	public function onAfterInitialise()
	{
		// exit();
	}
	
	public function onAfterRender()
	{
		// exit();
	}
}
The code in the /libraries/foo/foo.php file...

Code: Select all

defined('_JEXEC') or die;
class Foo
{
	public function someMethod() {
		return true;
	}
}
If I try calling it by var_dump(Foo::someMethod()); blank page!

The above code WORKS on localhost Xampp on Joomla 3.8.12 Stable but NOT online on 3.4.8 Stable

If somebody could help me with this it would really make my day. And by the way... The onAfterInitialise() in the plugin is not working either.

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

Re: JLoader::registerNamespace Not Working?

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

Fix the constructor:

Code: Select all

public function __construct(&$subject, $config)
{
	parent::__construct($subject, $config);
		
	JLoader::registerNamespace('Foo', JPATH_LIBRARIES . '/foo');
}
Or load your libraries later during onAfterInitialise:

Code: Select all

public function onAfterInitialise()
{
	JLoader::registerNamespace('Foo', JPATH_LIBRARIES . '/foo');
}

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 11216
Joined: Thu Feb 15, 2007 5:48 am
Location: Suzhou, China

Re: JLoader::registerNamespace Not Working?

Post by toivo » Tue Apr 09, 2019 8:29 am

General piece of advice: versions 3.8.12 and 3.8.4 have been replaced a while ago. The latest version of Joomla is 3.9.4 and version 4.0 is at alpha stage of development.
Toivo Talikka, Global Moderator

sazbaztaz
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Thu Mar 18, 2010 2:20 pm

Re: JLoader::registerNamespace Not Working?

Post by sazbaztaz » Tue Apr 09, 2019 8:51 am

Thats working SharkyKZ. Thank you! I did not know the onAfterInitialise() etc fires trough the constructor...


Post Reply

Return to “Joomla! 3.x Coding”