HomePHP Working With Different Namespaces in PHP 5
Working With Different Namespaces in PHP 5
Namespaces have been available for a long time in mature object-oriented languages like Java and C++. However, there’s good news on the horizon, since they will also be available in PHP 5.3 (hopefully). That’s a good reason to start learning how to use them within the context of object-oriented PHP applications. This is the second part of a four-part series that shows you how.
In this group of articles you’ll find an approachable guide to utilizing namespaces with PHP 5 classes. In addition to learning the theoretical concepts that surround this topic, you’ll be provided with numerous code samples to help you understand quickly how to use them in different cases.
And now that I introduced you to the main subject of this series of articles, it’s an excellent time to recapitulate very quickly the topics that I covered in the preceding tutorial. As you’ll surely recall, I left off that article discussing how to tie a given PHP 5 class to a specific namespace by means of the “namespace” keyword.
In that particular situation, I defined a sample “User” class, which was linked to a fictional “UserManagement::CMS” namespace via the aforementioned keyword, assuming that the class in question would be utilized in the context of a user-related content management system.
Of course, although this hands-on example was actually pretty basic and had a rather limited functionality, it was useful enough to demonstrate how to associate a determined PHP 5 class to a concrete namespace.
However, so far I've shown you how to work only with one class, tied to a specific namespace. Thus, in this second part of the series I will illustrate how to link two classes that share the same name to different namespaces. Doing so will show you more clearly the actual functionality of using distinct namespaces in the context of an object-oriented PHP application.
So, are you ready to continue learning more useful things about working with namespaces in PHP 5? Then let’s jump in!