PHP
  Home arrow PHP arrow Page 4 - 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 - Autoloading classes and working with custom functions
    ( Page 4 of 4 )

    As I said in the previous section, the last two topics that I plan to discuss in this last part of this article are related to using namespaces with the “__autoload()” PHP magic function, and with regular functions as well.

    First I’m going to show you a basic example of how to implement “__autoload()” when working with namespaced classes. Here’s a simple implementation for this magic function:

    // implements the __autoload() with namespaced classes


    function __autoload($className){

    $className=str_replace('::','/',$className);

    if(!file_exists($className.'.php')){

    eval("class $className {}");

    throw new Exception('Source class not found!');

    }

    require_once $className.'.php';

    }


    If you develop PHP applications that include source classes via the “__autoload()” function, then the above example should be pretty easy to understand. As you can see, I implemented this function in a fairly basic way, so it can include a class that’s been previously linked to a specific namespace.

    The function, in this particular case, assumes that the class has been passed in the following form:

    namespace::classname


    Thus, it first replaces the :: characters with a backslash, and then finishes its execution by attempting to load the class in question. Pretty simple to understand, right?

    Now that I have quickly explained how to include a class that has been tied to a namespace, please take a look at this final example. It shows how to define and use two simple PHP functions, where the first one is linked to a sample “MyOffice” namespace:

    namespace MyOffice

    // define function within the 'MyOffice' namespace

    function displayNameSpace(){

    return 'calling function from MyOffice namespace!';

    }

    // define function in the global namespace

    function displayNameSpace(){

    return 'calling function from the global namespace!';

    }


    echo MyOffice::displayNameSpace();

    // displays 'calling function from MyOffice namespace!'


    echo displayNameSpace();

    // displays 'calling function from the global namespace!'


    Indeed, this last example is very simple to follow, trust me. As you can see, first a trivial “displayNameSpace()” function is linked to a “MyOffice” namespace, and then another function that has the same name is declared in the global namespace. Since these functions are actually different, they’ll also generate distinct outputs.

    The following code snippet clearly demonstrates this process:


    echo MyOffice::displayNameSpace();

    // displays 'calling function from MyOffice namespace!'


    echo displayNameSpace();

    // displays 'calling function from the global namespace!'

    With this last code sample, I’m finishing this introduction to using namespaces with PHP 5. Please keep in mind that all of the code samples shown in this group of tutorials will work (hopefully) with PHP 5.3, and not with earlier versions.

    Final thoughts

    Sad but true, this is the end of the series. I hope that all the material included in these articles will help you grasp more quickly how to utilize namespaces with PHP 5. Indeed, they can be quite useful for solving naming conflicts between classes and functions, something that might occur incidentally when the development of a PHP application is tackled by several programmers.

    See you in the next PHP development tutorial!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek