Home arrow PHP arrow Page 2 - Utilizing the Use Keyword for Namespaces in PHP 5

Review: linking two sample classes to different namespaces - PHP

The release of PHP 5.3 is just around the corner. This version of the language comes with a lot of useful features that will put a smile on the face of many programmers. One major improvement it introduces is support for namespaces. This feature permits users to work independently with classes that share the same name, facilitating the development of object-oriented applications that use third-party libraries. Keep reading to learn more.

TABLE OF CONTENTS:
  1. Utilizing the Use Keyword for Namespaces in PHP 5
  2. Review: linking two sample classes to different namespaces
  3. Introducing the use keyword
  4. Working with an instance of the previous User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
November 26, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: