Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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, here is my code:

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() {
    dpm(array('this','is','a','test'));
    return 'this is a block';
  }
}

and it works fine. But what I don't understand is: 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()?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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 \).

share|improve this answer
2  
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 Jul 28 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? –  user1015214 Jul 29 at 12:37
1  
'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 Jul 29 at 13:58
    
...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 Jul 29 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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