Join Justin Yost for an in-depth discussion in this video PHP namespaces overview, part of Advanced PHP.
- [Trainer] Let's start our discussion of some advance techniques in PHP with namespaces. Have you ever had the problem of needing to name a class the same name as some other class somewhere else in your project? Or, the same name as some class in a library that you have in part of your project? This is a pretty common problem. You have some library that has a log class but you also want to write your own log class. PHP namespaces are the solution to this problem. Namespaces give us the ability to tell our code, "Okay, you can be named log "but only along this particular path." And you can also have the name log but it's along this other particular different path, such as /app/util/log.php.
And you can also have the name, log class, available at /vendor/framework/framework/log/log.php, and again, a third log class, available at /vendor/package/package/util/log.php. In essence, we get to tell our code, rather than use the first version of the log class you find, use this version along this particular namespace. But more than that, it's more than just separating code between both libraries and packages you are importing.
It can even be within your project itself. The namespace is just a way of us telling PHP, find this class here, so we can do this at any point. Using our examples of this /app/util/log.php class, we can tell it to find it at \App\Util\Log. And, again, we can tell PHP to find our particular framework log file at \Fw\Fw\Log\Log. And, again, a package can provide its own log class along a different namespace path. Let's explore the benefits of namespacing.
What specifically do we get out of it? One of the goals with using PHP namespaces is making easier for you to import packages in libraries. If you use PHP before namespaces, or never bothered with name spaces before, you may be used to seeing classes named, my awesome uploader log and my awesome table. These weird and not very useful names are trying to solve the problem namespaces deals with. Lots of people want to name classes table and log and other common names, but you can't have two classes named the same thing, prior to PHP namespaces.
However, namespaces aren't just for the names of the classes. Namespaces also allow you to have multiple functions titled, for instance, get or even provide your own versions of a core PHP method such as a Ray Map. Namespaces give us the ability to create multiple methods and projects that you use the same name without worrying about conflicts or collision. This feature, however, is only present in PHP 5.6 or above. Namespaces, in addition to allowing you to import classes from other parts of your project, also permit the ability to alias the class.
This allows you to import two classes that have the same name along different namespaces as two different names. For instance, we can import two collection classes, but alias one has collection Faker that was provided to us from the Faker Library and import collection from the CSb collection class. One of the other many benefits from using namespaces is we gain access to the whole world of open source software. And now it's much easier for us to include and drop into our project as needed.
Composer is a PHP package manager that let's us do just that. We can import and autoload packages and classes from hundreds of open source libraries and frameworks. And, all of them are equally accessible and useful in our core applications code. "Autoloading," you say, "what's that?" Autoloading gives us the ability to write one line in code and use that package regardless of the file path to that code. As an example, remember using require and include? Wasn't that awful? Having to go, "Okay, this is one level deep, "so I didn't need to do, require ../somefile.php, "but this other one is two levels deep and a folder away "so I need to do "require /../../lib/someotherfile.php." That was awful.
Autoloading replaces all of that work and sends seekers out how to find the exact path to the class without us having to do any of this crazy work. All you have to do with autoloading is say, "I want to use this class at this namespace path," and, boom, you get to use that class right away. So, let's learn how to use PHP namespaces in the next video.
Learn how to establish consistency, solve problems, and prevent your applications from crashing by applying the techniques Justin shares in this course. Take your object-oriented programming beyond basic attributes and methods into using constructors, deconstructors, and singletons. Build nested exceptions, use type hints, and explore additional ways you can craft more flexible software using PHP.
- Standard interfaces
- Constructors, deconstructors, and singletons
- Cloning objects
- Abstract classes
- Password hashing and verification
- Type hints, strict type hints, and return types
- Advanced closures
- Nested exceptions and SPL exceptions