I am new to namespaces and I am practicing with Drupal 8. I have created a custom module and have installed the contrib, devel.

Inside my module, I have defined a block.

namespace Drupal\twitter_pull\Plugin\Block;
use Drupal\block\BlockBase;

 * Provides a block for executing PHP code.
 * @Block(
 *   id = "twitter_pull_tweets_block",
 *   admin_label = @Translation("Twitter Tweets")
 * )
class TweetsBlock extends BlockBase {

   * Builds and returns the renderable array for this block plugin.
   * @return array
   *   A renderable array representing the content of the block.
   * @see \Drupal\block\BlockViewBuilder
  public function build() {
    return 'this is a block';

It works fine. Why do I have access to the devel functions? Where in my code do I include the namespace for devel that I should have access to dpm()?

1 Answer 1


Straight from the PHP Global space docs:

Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.

Devel's dpm() is declared in 'Global space', and since there's no other dpm() function declared in your namespace, the 'global' one is used (no need for the preceeding \).

  • 3
    Correct, additionally, functions are special and different from classes. Classes in the global namespace must be referenced with a leading \, while functions automatically fall back to the global namespace. What is however confusing is that if a function is not found (neither global or current namespace), then the error will include the current namespace.
    – Berdir
    Commented Jul 28, 2014 at 21:22
  • Very interesting. I thought namespaces were a requirement in drupal 8, but I see that the devel.module file has no namespaces and thats where the dpm function is located. Are namespaces only not a requirement when located in the .module file? Commented Jul 29, 2014 at 12:37
  • 2
    'Required' perhaps isn't the right word...Drupal uses a PSR-4 autoloader to discover classes based on a strict naming convention (PHP will be told to look for Drupal\my_module\SomeClass in /path/to/my_module/src/SomeClass.php, for example). For classes that aren't auto-discovered by way of annotation (like plugins) they don't theoretically have to be namespaced at all; PHP/Drupal wouldn't care as long as the class names are unique within the global space. Of course polluting the global space is bad, and modularising one's code is always a good thing, so always use it for classes...
    – Clive
    Commented Jul 29, 2014 at 13:58
  • 1
    ...as far as functions go, I've yet to come across a framework that namespaces functions. I'm sure there are valid edge-cases for it, but in an OO architecture it just doesn't make much sense. D8 is still a mash of procedural and OO code, so you'll still find a lot of globally scoped functions. The hook system doesn't lend itself too well to scoped functions either, the whole thing's a lot easier with global ones, so you could go so far as to say D8 has a requirement to not use namespacing for functions
    – Clive
    Commented Jul 29, 2014 at 14:23

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