PHP
  Home arrow PHP arrow Page 3 - Using Aliases and the Autoload Function with Namespaces in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Using Aliases and the Autoload Function with Namespaces in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-12-03


    Table of Contents:
  • Using Aliases and the Autoload Function with Namespaces in PHP 5
  • Review: the use keyword
  • Utilizing namespace aliases
  • Autoloading classes and working with custom functions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Aliases and the Autoload Function with Namespaces in PHP 5 - Utilizing namespace aliases
    ( Page 3 of 4 )

    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;

    try{

    // create new instance of 'BlogValidator' class

    $blogValidator=new BlogValidator();

    $blogValidator->validateString($_POST['fullname']);

    $blogValidator->validateInteger($_POST['age']);

    $blogValidator->validateEmail($_POST['email']);

     

    // create new instance of 'CMSValidator' class

    $cmsValidator=new CMSValidator();

    $cmsValidator->validateString($_POST['fullname']);

    $cmsValidator->validateInteger($_POST['age']);

    $cmsValidator->validateEmail($_POST['email']);

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek