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?

5
  • Namespacing your code is a great idea...but it's already been done better than that :) drupal.org/project/xautoload Commented Apr 30, 2014 at 17:14
  • BTW I meant namespacing classes is a good idea. Functions, not so much IMO. What problem are you trying to solve? Commented Apr 30, 2014 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? Commented Apr 30, 2014 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 Commented Apr 30, 2014 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. Commented Apr 30, 2014 at 22:19

1 Answer 1

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.