Home arrow PHP arrow Page 3 - Using Namespaces in PHP 5

Tying a PHP class to a namespace with the namespace keyword - PHP

Namespaces were created as part of PHP 5.3 to solve certain conflicts that can arise when two different classes are given the same name. This situation comes up more commonly than you might think. This four-part series introduces you to using namespaces to solve these and similar issues. It includes copious code examples to enhance your understanding.

TABLE OF CONTENTS:
  1. Using Namespaces in PHP 5
  2. The need to use namespaces in PHP 5
  3. Tying a PHP class to a namespace with the namespace keyword
  4. Working with a PHP class tied to a specific namespace
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
November 12, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: