Introducing Namespaces

Posted on Updated on

Image result for NamespacesIntroducing Namespaces

As you continue to create class libraries as well as use third-party class libraries created by other Developers, you’ll inevitably encounter a situation where two libraries use identical class names, Producing unexpected application results.

To illustrate the challenge, suppose you’ve created a web site that helps you organize your book Collection and allows visitors to comment on any books found in your personal library. To manage this Data, you create a library named Library.inc.php, and within it a class named Clean. This class Implements a variety of general data filters that you could apply to not only book-related data but also User comments. Here’s a snippet of the class, including a method named filterTitle() which can be Used to clean up both book titles and user comments:

class Clean {

functionfilterTitle($text) {

// Trim white space and capitalize first word

returnucfirst(trim($text));

}

}

Because this is a G-rated Web site, you also want to pass all user-supplied data through a profanity filter. An online search turned up a PHP class library called DataCleaner.inc.php, which unbeknownst to you includes a class named Clean. This class includes a function named RemoveProfanity(), which is responsible for substituting bad words with acceptable alternatives. The class looks like this:

class Clean {

functionremoveProfanity($text) {

$badwords = array(“idiotic” => “shortsighted”,

“moronic” => “unreasonable”,

“insane” => “illogical”);

// Remove bad words

returnstrtr($text, $badwords);

}

}

Eager to begin using the profanity filter, you include the DataCleaner.inc.phpfile at the top of the Relevant script, followed by a reference to the Library.inc.phplibrary require “DataCleaner.inc.php”; require “Library.inc.php”; You then make some modifications to take advantage of the profanity filter, but upon loading theapplication into the browser, you’re greeted with the following fatal error message:

Fatal error: Cannot redeclare class Clean

You’re receiving this error because it’s not possible to use two classes of the same name within the same script. Starting with PHP 5.3, there’s a simple way to resolve this issue by using namespaces. All you need to do is assign a namespace to each class. To do so, you need to make one modification to each file. Open Library.inc.phpand place this line at the top:

namespace Com\Wjgilmore\Library;

Likewise, open DataCleaner.inc.phpand place the following line at the top:

namespace Com\Thirdparty\DataCleaner;

You can then begin using the respective cleanclasses without fear of name clashes. To do so, instantiate each class by prefixing it with the namespace, as demonstrated in the following example:

<?php

require “Library.inc.php”;

require “Data.inc.php”;

use Com\Wjgilmore\Library as WJG;

use Com\Thirdparty\DataCleaner as TP;

// Instantiate the Library’s Clean class

$filter = new WJG\Clean();

// Instantiate the DataFilter’s Clean class

$profanity = new TP\Clean();

// Create a book title

$title = “the idiotic sun also rises”;

// Output the title before filtering occurs

printf(“Title before filters: %s <br />”, $title);

// Remove profanity from the title

$title = $profanity->removeProfanity($title);

printf(“Title after WJG\Clean: %s <br />”, $title);

// Remove white space and capitalize title

$title = $filter->filterTitle($title);

printf(“Title after TP\Clean: %s <br />”, $title);

?>

Executing this script produces the following output:

Title before filters: the idiotic sun also rises

Title after TP\Clean: the shortsighted sun also rises

Title after WJG\Clean: The Shortsighted Sun Also Rises

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s