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.