XH 1.7: Introduce Namespaces

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

XH 1.7: Introduce Namespaces

Post by cmb » Thu Feb 26, 2015 10:59 pm

Hello Community,

I'd like to suggest that we introduce PHP namespaces for XH 1.7. Mainly I have in mind to introduce the XH and Filebrowser namespaces, and to adapt the autoloader for general loading of properly namespaced classes. I'm having only humble experiences with namespaces in PHP, but it appears to be only beneficial to put all classes in a proper namespace (instead of prefixing them with a separating underscore as it's done since CMSimple_XH 1.6). That allows to omit the namespace inside the same namespace. For now there are only a few places where that would make a difference, but I'm quite sure there'll be more in the not too distant future (think about type hints, which would make sense as soon as we pass objects as function arguments, what's currently not done due to the value semantics of PHP 4 objects). For example, currently we have:

Code: Select all

class XH_PageDataRouter
{
    // ...
    public function __construct($h, $pageDataFields, $tempData, $pageData)
    {
        $this->model = new XH_PageDataModel( 
This can be rewritten as:

Code: Select all

namespace XH;

class PageDataRouter
{
    // ...
    public function __construct($h, $pageDataFields, $tempData, $pageData)
    {
        $this->model = new PageDataModel( 
Already a modest improvement, in my opinion.

To keep backward compatibility, we could adjust the autoloader to automatically create a class_alias for the underscore convention for each class that is loaded.

I have some doubts that it makes sense to put the XH_* functions into a namespace. The main problem is that we would have to add aliases for several of them for backward compatibility. Unfortunately, there is no function_alias() or something like that, so we'd actually need to write wrapper functions. This appears to have more drawbacks than advantages, especially as I think it is actually useful to structure the existing functions differently in the future (not so much for XH 1.7). If that's going to happen, we would be left with a big bunch of compatibility function wrappers.

Thoughts?

PS: Of course, we should add use XH; to all relevant core and internal plugin files, if we're going to introduce the namespace.
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: XH 1.7: Introduce Namespaces

Post by cmb » Mon Mar 30, 2015 12:59 pm

Hi everybody!

I propose this minimal patch. It adds namespace declarations to the class files only and makes use of this inside these files. The autoload is supposed to handle namespaced classes as well as the traditional underscore "namespacing". Nested namespaces are not supported yet.
cmb wrote: Of course, we should add use XH; to all relevant core and internal plugin files, if we're going to introduce the namespace.
Nonsense. `use XH` would be an error, because it is not allowed to import complete namespaces, but only individual items.
Christoph M. Becker – Plugins for CMSimple_XH

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: XH 1.7: Introduce Namespaces

Post by cmb » Tue Apr 21, 2015 7:57 pm

Done (r1547).
Christoph M. Becker – Plugins for CMSimple_XH

manu
Posts: 1086
Joined: Wed Jun 04, 2008 12:05 pm
Location: St. Gallen - Schweiz
Contact:

Re: XH 1.7: Introduce Namespaces

Post by manu » Wed May 06, 2015 9:10 am

just a fundamental question:
Per design all plugins are loaded even they are not used on a particular page. With autoloading all plugin classes are included too. Is this the way we want to go? Or should we have a subfolder classes/auto/ and classes/on_demand?

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: XH 1.7: Introduce Namespaces

Post by cmb » Wed May 06, 2015 11:10 am

manu wrote:Per design all plugins are loaded even they are not used on a particular page.
Yes. That's how the pluginloader works traditionally.
manu wrote:With autoloading all plugin classes are included too.
No. Only those classes that are actually used somewhere will be loaded. I.e. if a class is not referred to anywhere in the executed code it will not be loaded. So we already have load-on-demand. This can be verified by adding logging to XH_autoload():

Code: Select all

file_put_contents(__DIR__ . '/../autoload.log', $className . PHP_EOL, FILE_APPEND); 
Christoph M. Becker – Plugins for CMSimple_XH

manu
Posts: 1086
Joined: Wed Jun 04, 2008 12:05 pm
Location: St. Gallen - Schweiz
Contact:

Re: XH 1.7: Introduce Namespaces

Post by manu » Wed May 06, 2015 1:14 pm

Thanks for the clarification, Christoph.
manu

Post Reply