PHP Namespaces - good idea?

PHP Namespaces - good idea?

by Mark Johnson -
Number of replies: 7
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

One of the things that's always narked me when I'm making a moodle plugin is having to prefix my functions and classes with plugintype_pluginname_ to avoid potential name collisions with other plugins.

Given that PHP 5.3 has support for namespaces, which would eliminate this need while still avoiding collisions, and that Moodle 2.1 will require PHP 5.3, is it sensible to use namespaces in my plugins to avoid having to do this?

Potential problems that have occured to me include:

  1. Moodle might do something funky when including lib.php files that means namespaces will cause an issue
  2. There would need to be a standard namespace hierachy defined for plugins
  3. Some functions and classes (such as standard activity module functions) would still need to keep the current naming conventions.

Only 1 really applies to me since they're mainly in-house tools (although I'll be releasing them), and the plugins I'm thinking of don't rely on naming conventions for the functions like activities do.

Of course's there's also probably 4. A bunch of stuff I don't know about namespaces that makes this unfeasible smile

Average of ratings: -
In reply to Mark Johnson

Re: PHP Namespaces - good idea?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

My first, gut, reaction as a plugin developer is that we've just had a massive 'break all 3rd-party plugins' update with Moodle 2.0, it would seem a little hasty to jump into another such change.

Which is not to say it's intrinsically a good or bad thing - I've not really looked into namespaces enough to be able to comment further.

In reply to Davo Smith

Re: PHP Namespaces - good idea?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm certainly not suggesting that we do it in a way that breaks 3rd party plug-ins.   I'm really just wondering if it's a sensible thing to do within a plugin if it's the developer's cup of tea.

In reply to Mark Johnson

Re: PHP Namespaces - good idea?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I can't see any reason why you should not use namespaces in your own code, for example in your locallib.php file.

There seem to me to be two obvious naming conventions one could adopt:

1. namespace plugintype_pluginname;

or

2. namespace plugintype\pluginname;

Good grief. They went for \ as the sub-namespace separator. How hiddeous. I think that is a good reason for choosing option 1. here.

 

I think you should try it, and let us know how you get on.

Of course, the other option, which has been available for some time, is to do most of your plugin code in an object-oriented way. Than you only have to mention long prefixed names in a few places where you do constructor calls, for example new plugintype_pluginname_thing(...);

In reply to Tim Hunt

Re: PHP Namespaces - good idea?

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Good grief. They went for \ as the sub-namespace separator. How hiddeous. I think that is a good reason for choosing option 1. here.

Yeah, there's a brief explanation why in this video: http://www.vimeo.com/13768954 (it's the only single punctuation character that didn't cause parser conflicts). To be honest, the fact that it's an ugly character isn't enough to put the off, so I think I'll try option 1 smile

Of course, the other option, which has been available for some time, is to do most of your plugin code in an object-oriented way. Than you only have to mention long prefixed names in a few places where you do constructor calls, for example new plugintype_pluginname_thing(...);

I honestly hadn't thought of doing it that way, for some reason (except for blocks where it's neccessary).  Still, for the sake of experimenting I'll try it with namespaces and, as you say, let you know how it goes.  If it's painful, looks like The current option is the way forward.

In reply to Mark Johnson

Re: PHP Namespaces - good idea?

by mohamed alsharaf -

Moodle works fine with namespace. You can find my experiment plugin in github

There were 2 problems:

1. moodleform::get_form_identifier() I had to override this method to replace '\' with '_'

2. renderable classes can't be namespaced. It is not going to work with renderer_base::render(renderable $widget)

Other than the above 2 issues the plugin works fine with namespace.