Constant NAMESPACE_SEPARATOR

Having namespaces in PHP is great. Having "\" as a namespace delimiter is a bit ... inconvenient (but if there is someone who thinks it's cool and sexy, I add the "rant" tag to this post .;).

So here is the question:

Do you use the NAMESPACE_SEPARATOR constant in your code? As in the code below:

<?php if (!\defined('NAMESPACE_SEPARATOR') { \define('NAMESPACE_SEPARATOR', '\\'); } // if 

Pros:

  • according to DIRECTORY_SEPARATOR (which all of us use;)
  • Do not confuse with escaping (think of '\ Foo \ Bar', but '\\'. Foo '.' \\ '.' Bar ')
  • more readable (IMHO)
  • which enables us to write good, name-supported autoloaders
  • may resist another change if something terrible happens (as with ol'good '::' from PHP 6 alpha)
  • can hide the uniqueness of "\" as a namespace operator in a programming language from strangers;)

Minuses:

  • "The reason DIRECTORY_SEPARATOR is because the value is platform dependent and the namespace delimiter is not." (as stated in http://bugs.php.net/bug.php?id=43046 )
  • 19 characters instead of 1 (\) or 4 ('\\')
  • There are places where you cannot use this (fully qualified class names as default class variables), i.e.

     <?php class A { protected $sDefaultReporterClass = '\My\Namespace\DefaultReporter'; } 

So what do you think?

+6
coding-style php namespaces
source share
3 answers

I'm not sure I have your point of view ... PHP constants are not like macro preprocessor C. You cannot create a macro and replace it with an operator - it just won't work:

 <?php define('ADD', '+'); echo (3 ADD 5); // Parse error: syntax error, unexpected T_STRING ?> 

Even if it worked, what is the purpose? Hiding the language syntax so that it looks like another language that you are more familiar with is a waste of time, not to mention the fact that it’s more difficult for other coders to work on a project. If you think the X language looks cooler, well, it's just X code, not PHP :)

Update

Using a namespace delimiter in places where a string is required (such as autoloaders and callbacks) creates small difficulties when using single quotes, since the only places to escape are right before the quote or other backslash can be written as there is:

 $callback = 'Foo\Bar'; 

All other options look like unnecessary complexity to me:

 $callback = "Foo\\Bar"; $callback = 'Foo' . NAMESPACE_SEPARATOR . 'Bar'; $callback = "Foo{$namespace_separator}Bar"; 
+6
source share

Yes - the answer to your question. I use the self-defined NAMESPACE_SEPARATOR because

 str_replace(NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $classFileName); 

clearer to me than

 str_replace('\\', DIRECTORY_SEPARATOR, $classFileName); 

It can only be used in my autoloaders, but I want to quickly understand the code if I read it in a few years.

+6
source share

This problem is now more or less resolved using the new approach (PHP> 5.5) when addressing class names using class name resolution (in PHP RFC it was called class name scanning ).

Using strings filled with class names is still useful for all kinds of magic, especially for automatic loading using fully qualified class names, since hard-coded strings are error prone and difficult to detect for the IDE.

How does this answer the original question: sometimes the namespace delimiter constant is useful for very specific parts of your code, although I would find fewer places where I would really use it.

In your example, you prefer to use:

 <?php namespace My\Namespace; class A { protected $sDefaultReporterClass = DefaultReporter::class; // or protected $sAnotherReporterClass = \Other\Namespace\Reporter::class; } 

When the scale of this problem is so small, it will be used only in a certain part of the code; this probably would not legitimize the built-in PHP constant. If so, other operators, such as the scope operator, should be included as a constant. Constant PAAMAYIM_NEKUDOTAYIM for "::"? (Sorry for the small constant name phun)

An incomplete PHP function that might come in handy after this approach would be the magic namespace constant ::.

+1
source share

All Articles