PHP
  Home arrow PHP arrow Page 2 - Utilizing the Use Keyword for 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

Utilizing the Use Keyword for Namespaces in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-11-26


    Table of Contents:
  • Utilizing the Use Keyword for Namespaces in PHP 5
  • Review: linking two sample classes to different namespaces
  • Introducing the use keyword
  • Working with an instance of the previous User class

  • 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


    Utilizing the Use Keyword for Namespaces in PHP 5 - Review: linking two sample classes to different namespaces
    ( Page 2 of 4 )

    Before I start explaining how to utilize the "use" keyword to link a PHP 5 class to a specific namespace, it'd be pretty helpful to recall quickly how to perform the same task using the "namespace" reserved word instead.

    The following example, which was developed in the previous tutorial, shows how to associate two sample classes named "User" to distinct namespaces, and how to work independently with two instances of these classes within the same PHP script.

    Having said that, here is the complete set of source files required to get this introductory example working as expected:


    (definition for 'cmsuser.php' file)


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

    }

    }

    ?>



    (definition for 'bloguser.php' file)

     

    <?php


    namespace UserManager::Blog;

     

    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;

    }

    // get all user data

    public function getAll(){

    return 'First Name: '.$this->firstName.' Last Name: '.$this->lastName.' Email: '.$this->email;

    }

    }

    ?>



    (definition for 'index.php' file)


    <?php


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

    } 

    ?>


    Here you have it. As you can see, the previous example shows how to use two different "User" classes independently in the same PHP script. In this concrete situation, the first of these sample classes has been linked to a "UserManager::CMS" namespace, while the second one has been tied to another, defined as "UserManager::Blog."

    At this moment you should feel pretty satisfied, since you hopefully recalled how to use the "namespace" keyword to associate a couple of sample classes to distinct namespaces.

    Aren't you really feeling that way? Okay, I know that you're interested in learning how to perform the same process by utilizing the "use" reserved word. To learn the full details on how to achieve this, you'll have to 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek