HomePHP Utilizing the Use Keyword for Namespaces in PHP 5
Utilizing the Use Keyword for Namespaces in PHP 5
The release of PHP 5.3 is just around the corner. This version of the language comes with a lot of useful features that will put a smile on the face of many programmers. One major improvement it introduces is support for namespaces. This feature permits users to work independently with classes that share the same name, facilitating the development of object-oriented applications that use third-party libraries. Keep reading to learn more.
So, if you're a PHP developer who's searching for an approachable guide on using namespaces with PHP 5, you've come to the right place. Welcome to the third installment of the series entitled "An Overview on Using Namespaces in PHP 5." Comprised of four articles, this series walks you through the basics of working with namespaces in PHP 5-controlled environments, including the usage of the "namespace" and "use" keywords, and the implementation of namespaces aliases as well.
If you had the chance to read the preceding article of the series, then you're probably familiar now with using namespaces in PHP 5. In that tutorial I explained how to link to sample classes that shared the same name to different namespaces. The immediate advantage in accomplishing this task was that the classes in question could be used independently within the same PHP script, without getting any errors from the PHP engine.
Definitely, namespaces allow you to solve in an elegant way any naming conflicts between classes that are used in the context of the same application, and as you learned in previous tutorials, they can be declared precisely by means of the "namespace" keyword. However, linking classes to specific namespaces by way of it requires writing more PHP code, since its syntax is pretty lengthy.
Fortunately, PHP 5 provides the "use" keyword, which can be employed to tie a given class to a particular namespace by using a shorter notation. Therefore, in this third part of the series, I'm going to code some illustrative examples for you that demonstrate the correct utilization of the "use" keyword.
Now it's time to continue learning more useful aspects of using namespaces in PHP 5. Let's get started!