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

Utilizing namespace aliases - 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



As I mentioned in the introduction, PHP 5.3 will also let you work with namespace aliases in a very simple fashion. Indeed, this process is reduced to associating  meaningful words with a certain namespace, which will require writing less, but more intuitive, code.

To demonstrate a typical use of namespace aliases, I’m going to create for you a fictional scenario. In this example, two validation classes that share the same name are associated with different namespaces. The first of these namespaces will be used in the context of a blog application, while the second one will be associated with a content management system.

So, based on this hypothetical situation, the use of namespace aliases could be exemplified in the following manner:

use DataSanitizer::Blog::Validator as BlogValidator;

use DataSanitizer::CMS::Validator as CMSValidator;


// create new instance of 'BlogValidator' class

$blogValidator=new BlogValidator();





// create new instance of 'CMSValidator' class

$cmsValidator=new CMSValidator();





catch(Exception $e){

echo $e->getMessage();



As you can see from the above example, two aliases, called “BlogValidator” and “CMSValidator” respectively, have been linked to a couple of namespaces, which should be easy for you to follow. These aliases are tied to the aforementioned validation classes, which logically share the same “Validator” name. Simpler to code and read, right?

In this particular situation, I created a pair of validation objects to check the validity of user-supplied data, such as the user’s full name, age and email address. In this way you can see clearly how the objects in question can be utilized independently.

So far, so good. Now that you hopefully grasped the logic that stands behind using namespace aliases, I think that it’s time to explore a few additional features regarding the use of namespaces with PHP 5, since there are a couple of useful things that I’d like you to learn.

As you’ll recall, all of the examples developed in the previous tutorials (and the current one, actually) used classes that were defined along with the scripts that utilized them. However, there’s an important point that deserves a deeper analysis here. What happens if you want to include these classes via the “__autoload()” PHP magic function, as you’ve probably done before?

Or, to put the question more plainly: how can a class that’s linked to a certain namespace be automatically included into a PHP script with “__autoload()”? Well, it’s a pretty simple process really, even when it requires using a slightly special syntax.

Therefore, in the last section of this tutorial, I’ll be explaining in detail how to work with namespaces and the “__autoload()” magic function. And additionally, you’ll see how to link a simple PHP function to a specific namespace.

As usual, to learn how all of these tasks will be accomplished, you’ll have to click on the link that appears below 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: