Calling J! code from another app - namespace problem?

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

Moderators: ooffick, General Support Moderators

Forum rules
Locked
sclg
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 124
Joined: Mon Nov 06, 2006 7:46 pm
Location: Gloucestershire, UK

Calling J! code from another app - namespace problem?

Post by sclg » Thu Aug 23, 2018 4:20 pm

I have another (database) application that uses J! code to add a Joomla user when a new entry is created in the database. It uses a bit of code like this...
---------------
// Setup basic Joomla framework
define( '_JEXEC', 1 );
define('JPATH_BASE', "/home/phouse/public_html" );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$app = JFactory::getApplication('site');
$app->initialise();
// Now add array of the data and create user...
---------------
This all works fine. However the latest version of the database application has been 'namespaced' for the first time with
namespace xxx\yyy;
at the start of each file and thus, when I try and run it now, I get the error:
Fatal error: Class 'xxx\yyy\JFactory' not found in...

I understand why I'm getting the error but I'm not sure how to fix it. I suspect it may just need a suitable 'use' statement somewhere but I'm a 'namespace' beginner!
Any ideas please?
Last edited by toivo on Thu Aug 23, 2018 4:23 pm, edited 1 time in total.
Reason: mod note: moved to 3.x Coding

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

Re: Calling J! code from another app - namespace problem?

Post by SharkyKZ » Thu Aug 23, 2018 4:38 pm

Best option is to use namespaced Joomla classes. Add use statement and use real class name instead of aliased (typically removing J is enough but watch out for classes that have been renamed):

Code: Select all

use Joomla\CMS\Factory;

$app = Factory::getApplication('site');
Another options is to add a leading backslash to continue using Joomla classes from global namespace:

Code: Select all

$app = \JFactory::getApplication('site');
Third option is to use full namespaced classes (this is hard to maintain, not recommended):

Code: Select all

$app = Joomla\CMS\Factory::getApplication('site');
Last option, if you want minimal code changes for now is to import namespaced classes with old aliases. This only required adding use statements:

Code: Select all

use Joomla\CMS\Factory as JFactory;

$app = JFactory::getApplication('site');

sclg
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 124
Joined: Mon Nov 06, 2006 7:46 pm
Location: Gloucestershire, UK

Re: Calling J! code from another app - namespace problem?

Post by sclg » Thu Aug 23, 2018 5:22 pm

Thanks for that - Nearly there!

1.
It fixed the JFactory namespace problem but now gives a warning....

Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in /home/phouse/public_html/libraries/joomla/session/handler/joomla.php on line 48

Presumably something in the J! code is trying to set a session variable after the database app has already initiated one??

2.
Later in the same code I have a ...
$user = new JUser;
... so I need a 'use' for that as well. Is it Joomla\CMS\User ??

Thanks

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

Re: Calling J! code from another app - namespace problem?

Post by SharkyKZ » Thu Aug 23, 2018 5:46 pm

1. That seems to be the issue. The fix has been suggested but not yet implemented https://github.com/joomla/joomla-cms/pull/15742.

2. Yes, you have to do the same with all namespaced Joomla classes.


Locked

Return to “Joomla! 3.x Coding”