Home arrow PHP arrow Page 3 - Utilizing the Use Keyword for Namespaces in PHP 5

Introducing the use keyword - PHP

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



For those programmers who suffer from chronic laziness and want to use a short syntax, PHP 5 provides yet another keyword, called "use," which can be utilized to link one or more classes to a particular namespace.

In order to illustrate more clearly the utilization of this handy keyword, below I included another code sample. It shows how to tie the same "User" class that you saw in the previous section to a fictional "UserManager::Blog" namespace.

Here's how this example in question looks:

// example on using the 'use' keyword

use UserManager::Blog::User;


class User{

private $firstName;

private $lastName;

private $email;

public function __construct($firstName,$lastName,$email){


throw new Exception('Invalid First Name parameter!');



throw new Exception('Invalid Last Name parameter!');



throw new Exception('Invalid Email parameter!');






// get user's first name

public function getFirstName(){

return $this->firstName;


// get user's last name

public function getLastName(){

return $this->lastName;


// get user's email

public function getEmail(){

return $this->email;



As shown in the above hands-on example, the "use" keyword comes in handy for shortening the syntax required for declaring a namespace. In this case, a hypothetical "User::Manager" namespace has been linked to the sample "User" class that you learned before, but naturally this reserved word can be used with other classes.

Hopefully, the example that you just saw has been educational enough to demonstrate how to employ the "use" keyword in a useful manner when it comes to associating a concrete class to a particular namespace. However, I must admit that the example in question is still incomplete, since I haven't shown you how to call an instance of a class that's been linked to a namespace via the aforementioned keyword.

Therefore, the last section of this tutorial will be focused exclusively on explaining this process in depth. But, do you know how to get there? Click on the link shown below and keep reading!

>>> 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: