Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NAMESPACE_SEPARATOR constant

Having namespaces in PHP is great. Having '\' as namespace separator is a little bit ... awkward (but if there is someone who thinks that is cool & sexy, I am adding tag "rant" to this post. ;) .

So, here goes the question:

Are you using in your code NAMESPACE_SEPARATOR constant? As in code below:

<?php

if (!\defined('NAMESPACE_SEPARATOR') {

    \define('NAMESPACE_SEPARATOR', '\\');

} // if

Pros:

  • consistent with DIRECTORY_SEPARATOR (which all of us are using ;)
  • no mess with escaping (think of '\Foo\Bar' but '\\' . Foo' . '\\' . 'Bar')
  • more readable (IMHO)
  • which gives us in effect an opportunity to write good, namespace-aware autoloaders
  • can resist another change if something scary happens (as with ol'good '::' from PHP 6 alpha)
  • can hide uniquess of '\' as namespace operator in programming language land from strangers ;)

Cons:

  • "The reason for DIRECTORY_SEPARATOR is that the value is platform dependent, the namespace separator isn't." (as stated in http://bugs.php.net/bug.php?id=43046)
  • 19 characters instead of 1 ( \ ) or 4 ('\\')
  • There are places where you can't use this (full qualified class names as default class variables) ie:

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

So, what are you thinking ?

like image 998
ts. Avatar asked May 18 '10 07:05

ts.


People also ask

What is a namespace in coding?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is fully qualified class name PHP?

Fully qualified name. This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar . The namespace \Foo is also a fully qualified name. Relative name. This is an identifier starting with namespace , such as namespace\Foo\Bar .


3 Answers

I'm not sure I got your point at all... PHP constants are not like C preprocessor macros. You cannot create a macro and replace an operator with it—it just won't work:

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

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

Update

Using the namespace separator in places where it's required to be a string (such autoloaders and callbacks) offers little difficulty when using single quotes since the only places where it needs to be escaped is right before a quote or another backslash, thus it can be written as-is:

$callback = 'Foo\Bar';

All other options look to me like unnecessary complexity:

$callback = "Foo\\Bar";
$callback = 'Foo' . NAMESPACE_SEPARATOR . 'Bar';
$callback = "Foo{$namespace_separator}Bar";
like image 170
Álvaro González Avatar answered Sep 28 '22 15:09

Álvaro González


Yes is the answer to your question. I do use a self defined NAMESPACE_SEPARATOR because

str_replace(NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $classFileName);

is clearer to me than

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

It may only be used in my autoloaders but I want to quickly understand the code if I happen to read it a few years down the line.

like image 43
Ian Avatar answered Sep 28 '22 16:09

Ian


This issue is now more or less resolved by using a new (PHP >5.5) approach in addressing class names by using class name resolution (in the PHP RFC it was called class name scalars).

While using strings filled with class names is still useful for all kinds of magic, particularly auto-loading, using fully qualified class names as hard-coded strings are error prone and hard to detect for IDE's.

As how this answers the original question: Sometimes a namespace separator constant is useful for very specific parts of your code although I would come across less places where I would actually use it.

In your example you would rather use:

<?php

namespace My\Namespace;

class A {
    protected $sDefaultReporterClass = DefaultReporter::class;
// or
    protected $sAnotherReporterClass = \Other\Namespace\Reporter::class;
}

When the scope of this issue is so small it will only be used in a specific part of the code this will probably not legitimize a PHP build-in constant. If so, other operators like the scope operator should be included as a constant as well. The PAAMAYIM_NEKUDOTAYIM constant for "::"? (Sorry for the small constant naming phun)

A unimplemented PHP feature that could come in handy following this approach would be the magic Class::namespace constant.

like image 21
Joffrey Avatar answered Sep 28 '22 16:09

Joffrey