PHP
  Home arrow PHP arrow Page 2 - 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 - Linking a single class to a specified namespace
    ( Page 2 of 4 )

    Before I explain how to associate two classes that share the same name to different namespaces, it’d be a good idea to review the practical example built in the previous article of the series. It demonstrated how to perform this process with only one class.

    Basically, the code sample corresponding to the aforementioned example looked like this:


    namespace UserManager::CMS;

     

    class User{

    private $firstName;

    private $lastName;

    private $email;

    public function __construct($firstName,$lastName,$email){

    if(!$firstName||strlen($firstName)>32){

    throw new Exception('Invalid First Name parameter!');

    }

    if(!$lastName||strlen($lastName)>32){

    throw new Exception('Invalid Last Name parameter!');

    }

    if(!$email||!preg_match("/^.+@.+..+$/",$email)){

    throw new Exception('Invalid Email parameter!');

    }

    $this->firstName=$firstName;

    $this->lastName=$lastName;

    $this->email=$email;

    }

    // 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;

    }

    }


    try{

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

    $user=new UserManager::CMS::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 />';

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you can see, the above example shows a typical case where a single class has been tied to a concrete namespace. Speaking more concretely, in this case the previous “User” class has been linked to a “UserManagement::CMS” namespace by means of the pertinent “namespace” keyword.

    Finally, an instance of this class is created by using the following expression:


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


    That's pretty simple to understand, right? The actual functionality of using namespaces, however, is revealed when two or more classes that share the same name are included in one single PHP application. It’s probable that at this moment you’ll be wondering how this can be achieved without getting an error from the PHP interpreter.

    As you may guess, it’s possible to associate each of these classes with a different namespace, in this way solving any naming conflicts in an elegant manner.

    In the following section, I’m going to explain in detail how to perform this task. To learn how this will be done, please 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek