Home arrow PHP arrow Page 3 - Working With Different Namespaces in PHP 5

Linking two classes to different namespaces - PHP

Namespaces have been available for a long time in mature object-oriented languages like Java and C++. However, there’s good news on the horizon, since they will also be available in PHP 5.3 (hopefully). That’s a good reason to start learning how to use them within the context of object-oriented PHP applications. This is the second part of a four-part series that shows you how.

TABLE OF CONTENTS:
  1. Working With Different Namespaces in PHP 5
  2. Linking a single class to a specified namespace
  3. Linking two classes to different namespaces
  4. Working with objects that belong to different namespaces
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
November 19, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I mentioned in the section that you just read, it’s possible to link two classes that have been named the same to different namespaces, something that can be very useful in those cases where you’re working with third-party software.

In order to illustrate this concept, I’m going to create one sample file called “usercms.php,” which will include the “User” class that you saw before, along with the definition of the namespace where it will be used.

Having said that, here’s how this first sample file looks:


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

}

}

?>


So far there is nothing unexpected, right? As you can see, the previous “usercms.php” file first declares a “UserManager::CMS” namespace, and then  includes the signature of the pertinent “User” class.

Now that I already tied the previous “User” class to the “UserManager::CMS” namespace, I’m going to define a similar class. As you’ll see in a moment, this class will be linked to a different namespace. Of course, all of this code will be encapsulated into another sample file, called “bloguser.php,” whose definition is listed below:


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

}

}

?>


Certainly, things are getting really interesting now! As you can see, I built a second “User” class that extends the functionality of the first one. However, I’ve done this only for example purposes, since the most important detail to stress here is that both classes have been linked to different namespaces.

At this point, are you beginning to realize how useful namespaces can be when working with a group of classes that have the same name? I hope you are, because this is precisely the most relevant advantage in using namespaces in the context of an object-oriented application.

So far, so good. At this stage, you have hopefully learned, by means of the two previous sample files, how to tie two “User” classes to distinct namespaces. Thus, the next step will be aimed at demonstrating how the classes in question can be called independently within the same PHP script, even when they share the same name.

To see how this brand new script will be developed, please click on the link that appears below and read the following section. I’ll be there, waiting for you.



 
 
>>> 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: