Home arrow PHP arrow 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.

  1. Utilizing the Use Keyword for Namespaces in PHP 5
  2. Review: linking two sample classes to different namespaces
  3. Introducing the use keyword
  4. Working with an instance of the previous User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
November 26, 2008

print this article




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!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: