Namespacing Joomla Platform code for backwards/forward compatibility

139 views
Skip to first unread message

garyamort

unread,
Jun 21, 2012, 12:42:00 PM6/21/12
to joomla-de...@googlegroups.com
I was wondering when Joomla is going to advance to require PHP 5.3 only and if that can also include adding namespaces.

For example, all core code could be namespaced as Joomla\Platform
All cms code could be namespaced as Joomla\CMS

For backwards/forwards compatibility, aliases should be used:
use Joomla\Platform as J;
use Joomla\CMS as J\C;

So, for example:
$controller = JController::getInstance('Content');

Would become:
use Joomla\Platform as J;
$controller = J\JController::getInstance('Content');

Then for compatibility, some simple find/replaces on namespaced calls are all that is needed:
libraries/compat/joomla/v12.4
Change their namespace declarations:
namespace Joomla\Platform;
use Joomla\Platform as J; 
becomes
namespace Joomla\Platform\12.4;
use Joomla\Platform as J;
 
Now if an app for the CMS needs the 12.4 Controller model, it can use either:
use Joomla\Platform\v12.4 as J; 
or it could use
use Joomla\Platform\v12.4\JController as JController;  

Just the JController for 12.4 is used, all other classes will come from the current platform code even in JController since it always calls them as J\JLog 

Better yet, if there is a small subset of classes which someone might need for compatibility issues, 
only those can be stuck in libraries/compat/joomla/v12.4
For example, if ONLY the MVC classes are needed, then stick only their files in the compat folder.  A find/replace on those files for
J\JController, J\JModel, and J\JView to make them become JController, JModel, JView [and hence local classes]

So, things like
public function authorize($task)
{
// Deprecation warning.
J\JLog::add('JController::authorize() is deprecated.', JLog::WARNING, 'deprecated');

$this->authorise($task);
}

Will call the default Joomla\Platform namespace wheras
protected function createModel($name, $prefix = '', $config = array())
{
// Clean the model name
$modelName = preg_replace('/[^A-Z0-9_]/i', '', $name);
$classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);

$result = JModel::getInstance($modelName, $classPrefix, $config);

return $result;
}

Since the find/replace updated J\JModel to JModel it will be from the local space.

It is also great in that it allows both forwards and backwards compatibility.   Hypothetical:
Joomla CMS v5 is running on Joomla Platform v15.3
A lot of legacy extensions are still using the MVC framework of Joomla Platform 14.4
A lot of useful MVC features are in Joomla Platform 16.1

By containing directories with the MVC framework under namespaces
Joomla\Platform\v14.4
and
Joomla\Platform\v16.1

Legacy extensions may just need to change one line of code:
use Joomla\Platform as J;  
to
use Joomla\Platform\v14.4 as J;

And completely new extensions could be forward looking and use the new models with
use Joomla\Platform\v16.1 as J; 


Alternatively, going by the PHP Framework Interoperability Group standards:

It can be somewhat cleaner to say that the Joomla Framework is part of the global application space and only apply namespaces for compatability.

IE JController remains in the global namespace
For MVC compatibility:
Joomla\Platform\v14.4\JController 
Update all calls to classes starting with J to be \J, ie JLog becomes \JLog
Update all calls to MVC classes to remove the slash, so \JModel becomes JModel

This is somewhat more work for the compatibility libraries, but then it's also somewhat proper to make someone needing a different version of Joomla core libraries do the work and keep things simple for the core code.

Elin Waring

unread,
Jun 21, 2012, 5:10:47 PM6/21/12
to joomla-de...@googlegroups.com
The platform already requires 5.3 and that is the minimum which has been announced for CMS 3.0.  We are using J for both the CMS and Platform which works out well for developers and makes it possible to continue the easy movement of platform classes from Platform to Legacy to CMS .. and who knows maybe from CMS to Platform at some point.

Elin
Message has been deleted

Florian Voutzinos

unread,
Jul 23, 2012, 4:07:00 AM7/23/12
to joomla-de...@googlegroups.com
I think we need to drop the J prefix if Joomla is namespaced, because mixing both is bad (the goal of namespaces is also to avoid prefixes).

People won't have to change any call :

use Joomla\Factory as JFactory;

$app = JFactory::getApplication()...

It is even possible to parse their extension files and generate the correct use to place in the file headers, so they won't have to modify any call.

Chad Windnagle

unread,
Jul 23, 2012, 10:00:37 AM7/23/12
to joomla-de...@googlegroups.com
I was wondering when Joomla is going to advance to require PHP 5.3 only and if that can also include adding namespaces.

The platform already requires PHP 5.3, and it's been proposed that Joomla 3.0 will also require PHP 5.3.1 http://www.joomla.org/technical-requirements.html

Regards,
Chad Windnagle
Fight SOPA

Andrew Eddie

unread,
Jul 23, 2012, 2:10:10 PM7/23/12
to joomla-de...@googlegroups.com
On 21 June 2012 09:42, garyamort <gary...@gmail.com> wrote:
> I was wondering when Joomla is going to advance to require PHP 5.3 only and
> if that can also include adding namespaces.

We can address namespacing when the Platform is completely
auto-loadable. Trying to do it in bits and pieces is probably not the
best way to approach it.

Regards,
Andrew Eddie
http://learn.theartofjoomla.com - training videos for Joomla developers
Reply all
Reply to author
Forward
0 new messages