Home arrow PHP arrow Page 2 - Using Aliases and the Autoload Function with Namespaces in PHP 5

Review: the use keyword - PHP

Namespaces are an elegant approach to solving naming conflicts between the classes that are used by a PHP application. Indeed, it’s pretty common to suffer this problem during the development of a project that’s been charged to several programmers, and also when working with third-party libraries. This four-part series of articles, of which this is the last, shows you how to handle namespaces.

  1. Using Aliases and the Autoload Function with Namespaces in PHP 5
  2. Review: the use keyword
  3. Utilizing namespace aliases
  4. Autoloading classes and working with custom functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
December 03, 2008

print this article



I know that you want to see how to use namespace aliases with PHP 5, but first I’d like to reintroduce the example developed in the preceding article of this series. It was aimed at demonstrating how to utilize the “use” keyword to tie a sample “User” class to a fictional “UserManager::Blog” namespace.

That being said, here’s how the aforementioned example looked originally:

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;




// create new instance of 'User' class by using the specified namespace

$user=new User('Alejandro','Gervasio','alejandro@domain.com');

// display user data

echo 'First Name: '.$user->getFirstName().'<br />';

echo 'Last Name: '.$user->getLastName().'<br />';

echo 'Email: '.$user->getEmail().'<br />';

/* displays the following

First Name: Alejandro

Last Name: Gervasio

Email: alejandro@domain.com



catch(Exception $e){

echo $e->getMessage();



As you can see by the above code sample, linking a class to a concrete namespace via the “use” keyword is indeed a no-brainer process that can be tackled with minor hassles. Naturally, the main advantage in using this reserved word in place of “namespace” is that it permits the use of a shortened syntax, but other than that, both of them have the same functionality.

At this point, you've hopefully recalled how to associate a basic “User” class to a specific namespace, which means that it’s time to start explaining how to use namespace aliases to accomplish the same task.

This useful topic will be discussed in depth in the section to come, so jump ahead and read the next few lines.

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