All Drupal PHP code should be in it's own namespace.

By declaring the namespace at the top of each PHP file, it automatically prefixes functions and classes with 'Drupal\' . This means that the 'drupal_' prefix can be removed from function names and 'Drupal' removed from class names.

Advantages

  • It prevents namespacing collisions with functions and classes in other PHP software, e.g. #1370032: render() function defined by other PHP libraries.
  • We don't have to prefix our functions to prevent the above problem from happening. Currently some functions are prefixed and some are not.

    Prefixes are used for our PHP Wrappers , but because this is not the only use of prefixes it is not a reliable way to determine if a function is a PHP wrapper or not.

    It is not the case that it can be determined if a function is defined by PHP or Drupal by looking at the name. Most Drupal functions are not prefixed.

    Removing the Drupal prefix from all functions that are not PHP wrappers removes ambiguity. It also makes the code much more readable. We do not need to continually declare that every function is part of Drupal when we are in our own codebase!

  • It paves the way for potentially putting each module in it's own namespace.

I have managed to put all of Drupal 8 into it's own namespace with 100% of tests passing.

How it works

A script does most of the heavy lifting. This is followed by a patch that tidies everything up and ensures that all tests pass.

Both are attached. There are also two binary files attached. These files are gzipped PHP files and so couldn't be included in the patch and must be placed in core/modules/simpletest/tests/upgrade .

The script:

  • puts 'namespace Drupal;' at the top of every file.
  • imports any global classes that the file needs with the 'use' command.
  • removes 'drupal_' from function names except all PHP wrappers and
    • drupal_static() / drupal_static_reset() - this would be shortened to just 'static' which is a reserved word. In a way it can be considered a PHP wrapper.
    • drupal_render() - there are currently two functions: drupal_render() and render(). I couldn't determine how best to rename these functions.
    • drupal_goto() is renamed to redirect() because 'goto' is a reserved word. This also brings the terminology into line with the form api.
  • removes Drupal components from class and interface names
  • fixes drupal_static_reset() calls. The identifier is typically a function name and so must be prefixed with 'Drupal\'

The patch fixes all the tests. This primarily consists of fixing dynamic function calling - Drupal invokes a lot of functions like this, e.g. module hooks. Anytime a function or class name is referred to in a string, it must be prefixed with 'Drupal\' .

The testbot will fail because Drush does not understand namespaces.

I would encourage you to browse the consolidated patch or apply the patch and browse your favourite file to see what the changes look like.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Status: Needs review » Needs work

The last submitted patch, namespace-consolidated.patch, failed testing.

jbrown’s picture

Attaching updated patches that can be applied with git...

tim.plunkett’s picture

Status: Needs work » Needs review

So the testbot will run.

Status: Needs review » Needs work

The last submitted patch, namespace-consolidated.patch, failed testing.

jbrown’s picture

The bot wont test it as drush doesn't understand namespaces.

tim.plunkett’s picture

Status: Needs work » Needs review
FileSize
1.73 MB

Well, in this case it failed because of #1260716: Improve language onboarding user experience which was committed earlier today :)
Reroll. diffstat is the same.

tim.plunkett’s picture

Okay, so you're right, the testbot does fail pretty hard on this, but at least the patch is applied.
Is there an issue for this already?

jbrown’s picture

Nope.

jbrown’s picture

Updated for CamelCase Drupal namespace and reroll.

Status: Needs review » Needs work

The last submitted patch, namespace-consolidated.patch, failed testing.

jbrown’s picture

Reroll - again the testbot will fail because drush can't test namespaced code.

jbrown’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, namespace-consolidated.patch, failed testing.

jbrown’s picture

Status: Needs work » Needs review
donquixote’s picture

Look..
the problem I have with this:
Today we say that every file needs a "namespace Drupal;" -> biiiig commit, big noise in git history.
Tomorrow we say that core files need a "namespace Drupal\Core;" -> another big commit, making the previous one totally redundant.

What's the benefit, if we already know that just "Drupal" won't be the full story?

donquixote’s picture

Ok, if I understand correctly, then a subsequent patch (Drupal to Drupal\Core) would be easier, because a lot of work has already been done?

EDIT:
Yes, looks like there is really a lot of valuable work in here. Even if we change Drupal to Drupal\Core or something else, we can benefit from this work.

jbrown’s picture

Yeah - let's say hypothetically it was decided that all Drupal PHP code should be under namespace Drupal with non-module code being under Drupal\Core (and later Drupal\Component ) and that modules should be under namespace Drupal\Module\modulename .

First of all a patch from this issue would go in as it is the lowest common denominator.

Then a patch from another issue would put core into Drupal\Core (and put use Drupal\Core; into module files, prefix function calls from a module into core with Core\).

Then a patch from another issue would namespace module code. Core shouldn't be making any calls into modules, but this is not the case, e.g. use Drupal\Module\System will have to be put into some core files. Namespacing will make it clear that this is a problem to be rectified.

dkinzer’s picture

namespace-consolidated.patch queued for re-testing.

sun’s picture

Status: Needs review » Closed (duplicate)

Given the PSR-0 conversions already happening for D8, this issue looks obsolete to me.

donquixote’s picture

Status: Closed (duplicate) » Active

@sun,
I don't think so.
There is quite a disagreement (also on gdo) about whether procedural code and hooks need to be namespaced.
The PSR-0 discussions all explicitly say that they are all about classes, and don't care about procedural.

So, we could rename this issue or "wontfix" it, but it's definitely not a duplicate.

---------

For the namespaces for procedural code, the vital questions are:
1) How fragile is the current situation exactly? (for nameclashes)
2) How long will procedural code and procedural hooks be around in Drupal?
3) How important is it for us to make our procedural code cross-platform?
4) Is it worth the effort?

Some (webchick) say (on gdo, will add link some time later) that those nameclashes are a rather theoretical issue, and we should just name our modules properly (what that means, has yet to be said).

I imagine that a lot of them might go unnoticed, and could be responsible for some miraculous bugs that have been around forever.
I currently can think of exactly one case where a nameclash did happen (sth core vs contrib).

The bigger practical problem for me is the preemptive paranoia when naming modules and functions. E.g. if nowadays I had to redo admin_menu and menu_block, i would rather call them adminmenu and menublock, in fear of prefix collisions. This is pretty annoying.

RobLoach’s picture

We should rather leave our procedural code as it is and instead put in effort to "do it right" by moving them towards object-orientation. I'm with sun on this one. Namespacing public functions doesn't actually benefit us. It's like sticking lipstick on a pig.

donquixote’s picture

There is OOP code out there which is just as ugly as, or even worse than our procedural code.
For my taste, procedural can stay around for a much longer time, while we slowly migrate those things to OOP that actually benefit from it.

[EDIT 2023] Whether or not this was true at the time, I wrote it, we are far beyond this point. I no longer stand by this statement :) [/EDIT 2023]

In the meantime, I personally would be happy about namespaces in procedural and hooks, if only to fight my paranoia.

Besides, so far I have not seen a convincing OOP replacement for procedural hooks. We might see something emerge in D8 contrib, but this would still have to prove itself.

-----------

I suggest we keep this issue or another one as a place to discuss.

donquixote’s picture

Title: Namespace all Drupal PHP code » Namespaces for procedural code

Since OOP code is already dealt with elsewhere, I think we should rename this issue.

-----------

For those who don't see the point in doing that, here is an example I just ran into.

For a project I want to create (http://drupal.org/project/htmltag), I would like to have a
function htmltag_attributes()

This thing is supposed to be a simple factory, that should return an object of type htmltag_TagAttributes.
Now,
https://www.google.de/search?q=drupal+hook_attributes
it looks like hook_attributes() is a name already taken, although it is hard to tell if this is a regular hook, or something with a special mechanic (where my function might not count as an invocation).

And even if it's not, who knows if something in the future won't declare hook_attributes() as a regular hook.

This uncertainty makes me rethink the function name of "htmltag_attributes()", so I might name it "htmltag_tag_attributes()" instead, which is totally pointless, and just a wild guess that this won't ever collide with a hook.

donquixote’s picture

Title: Namespaces for procedural code » Namespace all Drupal PHP code (esp. procedural)

Hm, this rename was a bad idea. We already have plenty of those issues around, and renaming will only make them less distinguishable.

So, partial revert of the rename.

donquixote’s picture

Issue summary: View changes

drupal => Drupal

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: +Needs issue summary update

Wonder if the issue summary could be updated around D10.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.