PHP
  Home arrow PHP arrow Page 3 - 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 - Introducing the use keyword
    ( Page 3 of 4 )

    For those programmers who suffer from chronic laziness and want to use a short syntax, PHP 5 provides yet another keyword, called "use," which can be utilized to link one or more classes to a particular namespace.

    In order to illustrate more clearly the utilization of this handy keyword, below I included another code sample. It shows how to tie the same "User" class that you saw in the previous section to a fictional "UserManager::Blog" namespace.


    Here's how this example in question looks:


    // example on using the 'use' keyword

    use UserManager::Blog::User;

     

    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 shown in the above hands-on example, the "use" keyword comes in handy for shortening the syntax required for declaring a namespace. In this case, a hypothetical "User::Manager" namespace has been linked to the sample "User" class that you learned before, but naturally this reserved word can be used with other classes.

    Hopefully, the example that you just saw has been educational enough to demonstrate how to employ the "use" keyword in a useful manner when it comes to associating a concrete class to a particular namespace. However, I must admit that the example in question is still incomplete, since I haven't shown you how to call an instance of a class that's been linked to a namespace via the aforementioned keyword.

    Therefore, the last section of this tutorial will be focused exclusively on explaining this process in depth. But, do you know how to get there? Click on the link shown 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek