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.