PHP
  Home arrow PHP arrow Page 4 - Working With Different 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

Working With Different Namespaces in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-11-19


    Table of Contents:
  • Working With Different Namespaces in PHP 5
  • Linking a single class to a specified namespace
  • Linking two classes to different namespaces
  • Working with objects that belong to different namespaces

  • 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


    Working With Different Namespaces in PHP 5 - Working with objects that belong to different namespaces
    ( Page 4 of 4 )

    Since in the previous section you learned how to link two sample classes named “User” to distinct namespaces, it’s time to see how to use their instances within the same PHP script, without getting any errors from the PHP engine.

    That being explained, please take a look at the following code sample, which demonstrates how to display information about a couple of fictional users by utilizing the two namespaces declared earlier. Here it is:


    // include class files

    require_once 'bloguser.php';

    require_once 'cmsuser.php';


    try{

    // create new instance of 'User' class (belongs to UserManagement::CMS namespace)


    $cmsUser=new UserManager::CMS::User('Alejandro','Gervasio','alejandro@domain.com');

    // display user data

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

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

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

     

    /* displays the following


    First Name: Alejandro

    Last Name: Gervasio

    Email: alejandro@domain.com

    */


    // create new instance of 'User' class (belongs to UserManagement::Blog namespace)


    $blogUser=new UserManager::Blog::User('John','Doe','john@domain.com');

    // display user data

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

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

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


    /* displays the following


    First Name: John

    Last Name: Doe

    Email: john@domain.com

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    } 


    As you can see from the earlier example, I first created a pair of users for each “User” class, and then displayed some basic data about them on screen. Logically, the most interesting aspect to note here is how each of these classes are instantiated by means of their corresponding namespaces. Even when there are actually two classes named “User,” they belong to distinct namespaces. Pretty good, right?

    And with this last example, I have finished this humble introduction to linking two sample classes to different namespaces. As usual, feel free to use all of the code samples developed in this article, so you can extend your existing skills for using namespaces with PHP 5.

    Final thoughts

    In this second episode of the series, you hopefully learned how to tie a couple of basic sample classes that have the same name to different namespaces. As you saw before, this process is pretty intuitive, so you shouldn’t have major problems understanding how it works.

    In the upcoming article, I’m going to show you how to employ the “use” PHP keyword to declare several namespaces within the same application. Therefore, now that you’re aware of the topics that will be discussed in the next tutorial, you can’t miss it!



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