The modern PHP language has many exciting new features. Many of these features will be brand new to PHP programmers upgrading from earlier versions, and they’ll be a nice surprise to programmers migrating to PHP from another language. These new features make the PHP language a powerful platform and provide a pleasant experience for building web applications and command-line tools.
Some of these features aren’t essential, but they still make our lives easier. Some features, however, are essential. Namespaces, for example, are a cornerstone of modern PHP standards and enable development practices that modern PHP developers take for granted (e.g., autoloading). I’ll introduce each new feature, explain why it is useful, and show you how to implement it in your own projects.
I encourage you to follow along on your own computer. You can find all of the text’s code examples in this book’s companion GitHub repository.
If there is one modern PHP feature I want you to know, it is namespaces. Introduced in PHP 5.3.0, namespaces are an important tool that organizes PHP code into a virtual hierarchy, comparable to your operating system’s filesystem directory structure. Each modern PHP component and framework organizes its code beneath its own globally unique vendor namespace so that it does not conflict with, or lay claim to, common class names used by other vendors.
Don’t you hate it when you walk into a coffee shop and this one obnoxious person has a mess of books, cables, and whatnot spread across several tables? Not to mention he’s sitting next to, but not using, the only available power outlet. He’s wasting valuable space that could otherwise be useful to you. Figuratively speaking, this person is not using namespaces. Don’t be this person.
Let’s see how a real-world PHP component uses namespaces. The Symfony Framework’s own
symfony/httpfoundation is a popular PHP component that manages HTTP requests and responses. More important, the
symfony/httpfoundation component uses common PHP class names like
Cookie. I guarantee you there are many other PHP components that use these same class names. How can we use the
symfony/httpfoundation PHP component if other PHP code uses the same class names? We can safely use the
symfony/httpfoundation component precisely because its code is sandboxed beneath the unique
Symfony vendor namespace. Visit the
symfony/httpfoundation component on GitHub and navigate to the Response.php file. It looks like Figure 2-1.
Look closely at line 12. It contains this code:
This is a PHP namespace declaration, and it always appears on a new line immediately after the opening
<?php tag. This particular namespace declaration tells us several things. First, we know the
Response class lives beneath the
Symfony vendor namespace (the vendor namespace is the topmost namespace). We know the
Response class lives beneath the
Component subnamespace. We also know the
Response class lives beneath yet another subnamespace named
HttpFoundation. You can view other files adjacent to Response.php, and you’ll see they use the same namespace declaration. A namespace (or subnamespace) encapsulates and organizes related PHP classes, just as a filesystem directory contains related files.
Subnamespaces are separated with a
Unlike your operating system’s physical filesystem, PHP namespaces are a virtual concept and do not necessarily map 1:1 with filesystem directories. That being said, most PHP components do, in fact, map subnamespaces to filesystem directories for compatibility with the popular PSR-4 autoloader standard
Technically speaking, namespaces are merely a PHP language notation referenced by the PHP interpreter to apply a common name prefix to a set of classes, interfaces, functions, and constants.