PHP
  Home arrow PHP arrow Page 3 - 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 - Tying a PHP class to a namespace with the namespace keyword
    ( Page 3 of 4 )

    The simplest way to tie a PHP class to a specific namespace is by using the "namespace" keyword. Now, returning to the "User" class built in the previous section, if I ever want to link it to a fictional "UserManager::CMS" namespace, then I'd use the keyword in question in the following manner:


    // example on using the 'namespace' keyword to tied up a class to a particular namespace


    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;

    }

    }


    As you can see in the previous hands-on example, before including the definition of the previous "User" class, I used the "namespace" keyword to indicate to the PHP interpreter that that specific class must be tied to a hypothetical "UserManager::CMS" namespace, assuming that the class in question will be used within the context of a user-related content management system.

    Of course, I created this concrete namespace only for illustrative purposes, but naturally you can use others to suit your personal needs and preferences.

    Well, at this point I already showed you how to specify that a sample "User" class must belong to a "UserManager::CMS" namespace, in this way preventing any eventual conflicts with one or more classes that share the same name.

    However, the prior example would be incomplete if I didn't show you how to call this sample class in the context of a PHP script. Thus, in the last section of this tutorial I'm going to code a brand new example for you to dissipate any possible doubts about how to achieve this process in a simple way.

    Jump ahead and read the next few lines. We're almost finished!



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