Are PHP Namespaces Really So Bad?

By Craig Buckler

PHP namespacesPHP developers have been demanding namespaces for some time. As PHP applications have grown larger and more complex, namespaces have become essential to prevent code clashes.

My recent tutorials received a number of comments complaining about namespace implementation in PHP. The main issues were the syntax and the backslash character. Before I tackle those issues, let’s take a quick look back at the history of PHP.

PHP is a Mess

Languages such as C# and Java were designed and follow rigorous syntax standards. PHP has evolved. The original version was released in 1995 and, by version 3, it was a popular procedural programming language. Version 4 introduced rudimentary object orientation and version 5 provides a reasonably standard OOP model. Namespaces have now been added to version 5.3.

PHP critics will argue that the language is a mess. Function names are inconsistent (e.g. strpos, str_split, substr), object handling has been tagged on, and some of the syntax is different — if not bizarre — when compared with other languages.

However, PHP remains the most widely-used server-side development language. Its versatility is one of its primary strengths:

  • Novice developers can start with simple procedural programming. They need never touch OOP techniques and can still be productive.
  • Code written 10 years ago in PHP 3 still works today in PHP 5.3. A few minor tweaks may be required, but major rewrites are rarely necessary.

PHP code may not always be pretty, logical, or elegant, but development is rapid and often easier to comprehend than the alternatives.

PHP Namespace Implementation

Unlike C# and Java, PHP has to retain compatibility with non-namespaced code. That has been achieved and you can choose whether to use namespaces or not. However, if you’re using PHP 5.3 (or above), I would recommend them — even if you simply use the same name throughout the whole of your project.

The choice of namespace and use as namespace operators seems logical. Some developers may disagree, but that would have been the case no matter what they’d been named. eTor

Finally, we come to the backslash character. Most critics complain that it’s ugly, difficult to read, and awkward to type on a Mac. However, I still consider it preferable to the double-colon that was originally proposed. Examine the following static method call:

// PHP 5.3 beta static method call
echo ::App::Lib1::MyClass::WhoAmI();

// PHP 5.3 final static method call
echo AppLib1MyClass::WhoAmI();

The second line is quicker to type, less error-prone, easier to read, and simpler to understand. If you see a backslash outside of a string, you know namespacing must be involved.

Of course, it would be great if PHP used a ‘.’ period for public methods, static methods, and namespaces. That would make it consistent with Java, C#, JavaScript, Python and many other languages. Unfortunately, PHP’s history and backwards compatibility makes that difficult to achieve.

No language is perfect, and PHP is far from it! However, namespacing has been implemented well, especially when you consider the restrictions and problems it could have caused. I’m sure you’ll learn to love that backslash!

Related reading:

  • still seriously

    it would not be as much of a problem if PHP’s syntax was not so bankrupt and far-gone. If you compare PHP’s syntax to say C, you’ll note that their object notation of -> is actually used in C for *pointers* which PHP does not actually do. Then they used dots exclusively for string concatenation which really hurts their language design.

    Namespaces are wonderful, but I’d be pissed as well if I had to write a double colon for a namespace reference in C++ syntax. Scripting languages are supposed to be simple and elegant, and PHP is turning into a bigger mess with every release.

  • Stormrider

    I agree there’s way too much fuss about the backslash character. Who cares what character they use, as long as it doesn’t cause inconsistencies / ambiguity?

  • Michael Kimsal

    Code written 10 years ago in PHP 3 still works today in PHP 5.3. A few minor tweaks may be required, but major rewrites are rarely necessary.
    Code written in Java 10 years ago generally still works with current Java as well.
    Languages such as C# and Java were designed and follow rigorous syntax standards. PHP has evolved.
    This is a bit of a weak defense of the current state of PHP. C# and Java and other languages evolve too. Additionally, there *is* a modicum of design going on in the PHP mailing lists, but there isn’t a single group or person responsible for the final decisions (like Guido in Python or Linus with Linux). This lack of a benevolent dictator has, imo, contributed to the current messy state of PHP.

    The namespace separator issue is interesting, because the bulk of the comments for it centered on aesthetics, number of characters of typing, ‘confusion’ is other characters are used, etc. I believe the issue was rooted much deeper, and that the current ZE couldn’t be extended to support a syntax like :: without causing a host of other problems. But rather than addressing that issue and looking towards revamping the underlying engine, we ended up with as a separator, which feels more like a band-aid than a real solution.

  • boen_robot

    A thing which PHP critics appear to oversight is PHP’s dynamical nature. If once upon a time it used “+” for both concatenation and addition, it would be in the same bad (IMO) spot in which JavaScript is now. The usage of the dot therefore eliminated it’s possible usage at most other places, among which is object navigation. They chose the next best thing – “->”, which is used in C++ about referencing pointers and therefore dynamically created, late binded objects (with the “new” operator). Since PHP uses only late binding (AFAIK), this is actually completely logical (IMO).

    As for namespaces, considering that C++ uses “::” (and that I’m familiar with it), I admit I too cringed at first from the thought of “”, but as said already, if it doesn’t cause inconsistencies, it’s not that big of a deal.

  • Charles

    whiners, write your own language. or use a different language if it’s so nasty. you don’t have to use the latest version. go back to using the original versions of PHP or something. whhaaa..whaaa..whaaa.


  • KCChiefs

    PHP Sucks, try a “real” programming language (C#/Java). Open-source will ALWAYS fail…

  • Greg Beaver

    Michael Kimsal: it’s very easy to say this:

    I believe the issue was rooted much deeper, and that the current ZE couldn’t be extended to support a syntax like :: without causing a host of other problems. But rather than addressing that issue and looking towards revamping the underlying engine, we ended up with as a separator, which feels more like a band-aid than a real solution.

    but it is also arrogant unless you have actually taken a look at the problem that :: causes. It has nothing to do with the Zend Engine – it has to do with the expectations a user would have.

    if you run code like this:


    How would you expect it to execute? It could either be a function baz() in namespace foo::bar, or a class bar in namespace foo, static method baz(). Now let’s look at this:

    function __autoload($class)
    include str_replace('::','/',$class);

    How should this code resolve if both static class method foo::bar::baz() and function foo::bar::baz exist? The existing solution was “if the function foo::bar::baz exists, use it, otherwise try the class.” However, the zinger is that if the above code were to load class foo::bar, and then a later included file defined function foo::bar::baz(), php would suddenly start using the function. This silent switch could be exploited as a security hole by systems that allow untrusted third-party plugins, even with a whitelist of allowed functions because it would allow “replacing” existing static class methods at run-time.

    C++ gets around this problem because it is compile-time. Other dynamic languages like Python don’t have custom autoloading.

    Because PHP has both a custom class autoloader feature, static methods, and functions, it is a unique language. Once this essential point was realized, the choice was obvious: find a separator that is different from ::, or something else that clearly delineates the boundary between namespace and element.

    Your suggestion of “revamping the underlying engine” really means “removing functions, or removing __autoload”

    In fact, removing functions from namespaces was seriously investigated, and turned out to be impossible without introducing wtf (do you have a parse error? ignore the namespace declaration for functions?)

  • Someone

    Ok I will bite.

    KCChief you do know that Java is an “open source” programming language right?

    Bloody trolls.

    Honestly I think the PHP namespace is ugly, but I will take it. PHP isnt perfect by a long shot, but you can be very productive in it very quickly. I recently went from Django back to PHP (requirements) and was suprised when I found myself just as productive in it.

  • secoif


    Open-source will ALWAYS fail…

    wtf. Since when? Try stop being a bigot so you stop sucking.

  • Craig Buckler

    Er, I think you’ll find that Java is now open-source. C# is also an open standard – that’s why it’s been adopted by the open source Mono project. Apache – the most successful web server, and MySQL – the most popular DB are open source too.

    The success of a programming language or system is rarely related to it’s commercial or open source roots.

    PHP may be clunky compared to, say, C# but you can still achieve the same goals. Besides that, ASP.NET is fairly sucky in the way it abstracts the web’s client-server architecture into a Windows-like event model.

  • Michael Morris

    KC is just a troll folks, why give him bites?

  • Anonymous

    The difference between Java and PHP is that

    1) Java is not entirely open source, some parts are still untouchable and hidden
    2) Java is still strictly controlled by an interested corporation

    #2 is especially important. There is no committee for Java. Sun decide what goes into Java, or someone on behalf of Sun in an official capacity.

    Also, C# is not open source, MSIL is open standard which means it’s now legal to write 3rd party compilers for C#. Nobody but Microsoft can write and contribute to C# and MSIL. All you can do is use it and not get sued. Also, Mono was doing C# long before it was an open standard.

  • objegargo

    This look interesting,so far.
    If it’s not just all bots here, let me know. I’m looking to network
    Oh, and yes I’m a real person LOL.


  • Amenthes

    However, namespacing has been implemented well…

    Sure, it has been implemented well for classes and constants. Functions and variables were somehow forgotten. It’s really stupid to have namespaces but not being able to use a function.

    Every time someone will want to use a namespaced function, outside of its own namespace, will have to do something like this:


    use somelongnamespace as prefix;



    How was that done before? By simply prefixing every function name with a common prefix, like prefix_call_function();

    Great achievement, indeed.

    In the case of variables things can be worked around as we’ve got lambas too.


    namespace somelongnamespace;

    // JavaScript module pattern
    call_user_func(function () {
    $var = 1; // this variable is local to the function.

    // This will be available as somelongnamespacefoo();
    function foo () {



    In my opinion, this implementation of namespaces is halfway done. It’s indeed better that we don’t have a double colon as a namespace separator, but… that’s not enough.

  • Amenthes

    By the way, you’re comment preview works “wonders”…

  • KCChiefs

    Ahhhh. The open-source Nazi’s are at it again. Also, PHP and Open-Source still suck…good day nerds….

  • KCChiefs

    Also, good luck getting a job with “hobby” PHP, their are none for ya, no employer respects PHP.

  • STLRams

    The chiefs suck and so do you.

  • Wardrop

    I personally have grown to really like the blackslash character as a namespace seperator. It looks neat in my opinion, as it resembles a directory structure. I personally don’t think they could have chosen a better character.

    In regards to PHP being a mess. Most languages suffer growing pains, and PHP is certainly feeling that pain at the moment. The good news is that the team behind PHP are quite aware of this, which is why in PHP 6 there tidying up things like function name inconsistencies.

  • Filme Downloaden

    I cannot believe this is true!

  • aleks discodust

    Also, good luck getting a job with “hobby” PHP, their are none for ya, no employer respects PHP.


    worst troll ever.

  • jgd12345

    @Craig Buckler

    Just to point out that you’re talking about ASP.NET Webforms. ASP.NET MVC may be more appealing. The thing i like about ASP.NET is that they provide the structure in Webforms and MVC instead of leaving this open to the developer. With PHP you often get all sorts of coding styles and is often difficult to understand someone elses code.

  • Craig Buckler

    Yes, I was referring to web forms. MVC should improve matters, but it’s not available yet. I don’t really think web forms provide any real structure – they just make you do things in a convoluted way if you understand what’s going on underneath the surface (or want custom HTML elements, more than one form, etc). C# can still be written in a variety of ways, so that’s no real advantage over PHP. It is a more elegant language, but shoddy code is shoddy code.

Get the latest in Front-end, once a week, for free.