How namespaces/PSR-0 got in Drupal 8
At time of writing, Drupal 8 is still at least 6 months away. But there are already quite a lot of examples available how things are going to look. One of the things which is going to change is the way PHP code is written. Drupal 8 will use namespaces and PSR-0 as a standard and will large follow common PHP conventions for OOP code. This post show some background on how some these things got into core, as far as I can find out from various issues, posts and discussions attended at DrupalCons.
Namespaces / PSR-0 in Drupal 8
I myself am not a core developer, but I do develop modules for Drupal, and a couple of hours before writing this blog post, I was just reading another post explaining annotations in D8. But I realized I didn't know much about the broader set of coding changes introduced in Drupal 8 and the code didn't make much sense to me. So I started digging into issue queues and core developer discussions about these things. Namespaces looked like fine place to start and I found an issue from way back in August 2009 discussing PHP namespaces as an alternative way for the hook system in Drupal.
That issue is postponed to Drupal 9, because changing the hook system is out of scope for D8 as you will read below, but it was followed by a number of other discussions:
- Use PHP 5.3 namespaces (july 2010)
That issue got neglected because the discussion continued at DrupalCon Chicago:
- Proposal for fixing PHP namespacing in Drupal 8 (March 2010)
After that, I think the discussion went quiet down for a while when everybody was busy getting Drupal 7 done (January 2011), but it was continued in 2011:
- Move all module-provided classes to PHP namespaces (PSR-0 or similar), and autoload them without the registry [policy, no patch] (September 2011)
That issue also covered other related discussion about how Drupal should handle autoloading modules, which is closely related to namespacing. After a while, at comment #209, Dries made a decision to adopt PSR-0 as a convention.
Meanwhile, in 2012, other related discussions about this took place:
- Namespace all Drupal PHP code (esp. procedural)
- Improving Our Use of PHP Namespaces
- Proposal: PHP 5.3 Namespaces for module-provided functions and hook implementations.
Hooks in Drupal 8
And hooks? Well the original discussion from 2009 was about the hook system and some developers would like to use Symfony's EventDispatcher as a replacement. But that won't get in for D8.
As things look now, we will have two parallel ways of doing things: the traditional hook system and Symfony Events and Subscribers/Listeners. That isn't necessarily a bad thing I guess.
BTW: With the namespacing changes, I'm still not sure if modules and themes will be allowed to have the same name in Drupal 8 now...