0

I've been getting into PHP namespaces and had a thought about how to clean things up a bit in my custom modules. I was thinking that for hooks I could use the traditional Drupal method of defining functions in the global namespace with the module name prefix but for all other functions and classes I could define them inside a PHP namespace. So, for my module "donut_win" a file would look like this:

<?php
namespace {

function donut_win_menu()
{
  $items['donut/win/%'] = array(
     'title'           => 'Hey, there!'
    ,'description'     => "Eveyone wins when donut wins."
    ,'page callback'   => 'Donut\\Win\\render_page'
    ,'page arguments'  => array(1)
    ,'access callback' => true
    ,'type'            => MENU_CALLBACK
  );

  return $items;
}

function donut_win_cron()
{
  # magic!
}

} # namespace global

namespace Donut\Win {

function render_page ($bingo)
{
  # so much magic!
}

}

I'm interested in what potential pitfalls would be with this method and any other disadvantages or advantages. Is this a good idea or am I crazy?

  • Namespacing your code is a great idea...but it's already been done better than that :) drupal.org/project/xautoload – Clive Apr 30 '14 at 17:14
  • BTW I meant namespacing classes is a good idea. Functions, not so much IMO. What problem are you trying to solve? – Clive Apr 30 '14 at 20:35
  • Why isn't namespacing functions a good idea? Mainly to avoid having to use the module prefix in code within that module (or namespace). I mean, the whole point of the Drupal prefix convention is to provide namespacing. Why not use PHP's built in namespacing features when possible? – donut Apr 30 '14 at 21:18
  • 1
    I meant that purely from a Drupal (not language) point of view. Drupal has it's own coding standards which it's best to follow, and namespacing functions isn't part of that. If you're not bothered about the Drupal conventions, then sure, do whatever works for you...but if you're looking to write a module to contribute back to the community, or want it to be understood easily by other Drupal devs in the future, it's best to stick to standards – Clive Apr 30 '14 at 22:01
  • That makes more sense. I agree about writing modules for general consumption. I'm in a situation where I'm writing a lot of very specific modules and am considering using this pattern. Thanks for the responses. – donut Apr 30 '14 at 22:19
0

It looks like there aren't any technical pitfalls. But, using PHP namespacing is not part of Drupal's coding standards. Clive sums it up nicely in a comment on the question:

Drupal has it's own coding standards which it's best to follow, and namespacing functions isn't part of that. If you're not bothered about the Drupal conventions, then sure, do whatever works for you...but if you're looking to write a module to contribute back to the community, or want it to be understood easily by other Drupal devs in the future, it's best to stick to standards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.