PHP
  Home arrow PHP arrow Page 2 - Using 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 Namespaces in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2008-11-12


    Table of Contents:
  • Using Namespaces in PHP 5
  • The need to use namespaces in PHP 5
  • Tying a PHP class to a namespace with the namespace keyword
  • Working with a PHP class tied to a specific namespace

  • 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 Namespaces in PHP 5 - The need to use namespaces in PHP 5
    ( Page 2 of 4 )

    A good way to start explaining how to utilize namespaces with PHP 5 consists of recreating a simple - yet potentially real -- situation, where the name of a given class can be shared by another. In this case, suppose that there's a class called "User" which stores common user-supplied data, such as first and last names, as well as the corresponding email addresses.

    The prototypical signature of a class like the one described above would look like this:


    // define 'User' class


    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;

    }

    }


    Undoubtedly, the signature of the above "User" class is quite easy to grasp, since this class simply stores the full name and the email address of a fictional user on a few properties. In addition, the class implements some accessors, used directly for retrieving the values of these properties when required.

    Now that you've seen how this sample class has been defined, here's an example that demonstrates how to use in a concrete case. Take a look at the following code sample, please:

    // example on using the previous 'User' class without namespaces


    try{

    // create new instance of 'User' class

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

     

    /* displays the following output

    First Name: Alejandro

    Last Name: Gervasio

    Email: alejandro@domain.com

    */

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Actually, I don't want to be accused of being too selfish, but as you can see in the previous example, I utilized the "User" class to save my first and last names, as well as my email address as plain class properties. Once that task has been accomplished, these values are echoed to the browser via the respective accessors. That was really simple to code and understand, wasn't it?

    So far, everything looks pretty good here, since this basic class does a decent job, particularly when it comes to handling user-supplied data. However, this seemingly peaceful scenario can turn quickly into a raging storm if I decide to work in conjunction with a third-party content management system, which also includes a class named "User."

    How can these classes be used within the same PHP application, without having to waste time renaming one of them?

    Well, the answer to that question is simply by using namespaces! With namespaces, it would be possible to specify that one "User" class belongs to a concrete namespace, called for example "General," while the second one would belong to a different namespace, named "CMS." Are you starting to see the benefits of using namespaces? I bet you are!

    However, if you're anything like me, then you'll want to see how namespaces can be used with some functional PHP code. Therefore, bearing in mind this possibility, in the section to come, I'm going to show you how to utilize the previous "User" class so it can be tied to a predefined namespace.

    To learn the full details of how this process will be accomplished, please click on the link that appears below and keep reading.



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