PHP
  Home arrow PHP arrow Page 4 - 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 - Working with a PHP class tied to a specific namespace
    ( Page 4 of 4 )

    Provided that in the previous section you learned how to link a basic "User" class to a specific namespace, the next thing that I'm going to show you in this tutorial will be how to call an instance of that class within a PHP script.

    The following script demonstrates in a simple way how to work with a "$user" object whose originating class has been tied previously to a "UserManager::CMS" namespace. Have a look at it, please:


    <?php


    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();

    }


    ?>


    Definitely, the most important thing to note here is how an instance of the "User" class is called within the previous script. The following statement:

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

    creates a new $user object, where the name of the class is preceded by the corresponding namespace. Also, you should note the use of the (::) operator, which indicates that the class in question is linked to the "UserManager::CMS" namespace.

    In this case, I used the "namespace" keyword to associate a sample class to a given namespace, which can be longer to code. There are a few additional methods in PHP 5 that permit you to perform the same task, but they'll be properly reviewed in upcoming tutorials of the series.

    For now, study the code samples developed previously to acquire a more solid background in using namespaces in PHP 5. And keep in mind that this feature will be available hopefully with the release of PHP 5.3.

    Final thoughts

    In this first part of the series, I provided you with a quick introduction to using namespaces in PHP 5. As you saw for yourself, using this feature is indeed a straightforward process that can be grasped with minor hassles.

    In the next tutorial, I'm going to show you how to work with two classes that share the same name, but are linked to different namespaces. Now that you're aware of the subject of this forthcoming article, you won't want to 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek