NAMESPACE_SEPARATOR constant

Posted by ts on Stack Overflow See other posts from Stack Overflow or by ts
Published on 2010-05-18T07:55:37Z Indexed on 2010/05/18 8:00 UTC
Read the original article Hit count: 409

Filed under:
|
|
|
|

Hello,

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:

    class A {

    protected $sDefaultReporterClass = '\My\Namespace\DefaultReporter';

    }

So, what are you thinking ?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5