PHP 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.
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:
1 // PHP 5.3 beta static method call
2 echo ::App::Lib1::MyClass::WhoAmI();
4 // PHP 5.3 final static method call
5 echo \App\Lib1\MyClass::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.
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!
- How to Use PHP Namespaces, Part 1: The Basics
- How to Use PHP Namespaces, Part 2: Importing, Aliases, and Name Resolution
- How to Use PHP Namespaces, Part 3: Keywords and Autoloading