Presenting Namespaces

Posted on Updated on

Image result for phpPresenting Namespaces

As you keep on creating class libraries and utilize outsider class libraries made by other Engineers, you’ll unavoidably experience a circumstance where two libraries utilize indistinguishable class names,Creating surprising application comes about. To show the test, assume you’ve made a site that helps you arrange your book Gathering and permits guests to remark on any books found in your own library. To deal with this Information, you make a library named Library.inc.php, and inside it a class named Clean. This class Actualizes an assortment of general information channels that you could apply to book-related information as well as Client remarks. Here’s a piece of the class, including a strategy named filterTitle() which can be Used to tidy up both book titles and client remarks:

class Clean {

workfilterTitle($text) {

/Trim white space and underwrite first word

returnucfirst(trim($text));

}

}

Since this is a G-evaluated Web webpage, you additionally need to pass all client provided information through an irreverence channel. An online inquiry turned up a PHP class library called DataCleaner.inc.php, which unbeknownst to you incorporates a class named Clean. This class incorporates a capacity named RemoveProfanity(), which is in charge of substituting terrible words with satisfactory choices. The class resembles this:

class Clean {

workremoveProfanity($text) {

$badwords = array(“idiotic” => “silly”, “simpleton” => “irrational”, “crazy” => “strange”);

/Remove terrible words

returnstrtr($text, $badwords);

}

}

Anxious to start utilizing the irreverence channel, you incorporate the DataCleaner.inc.php record at the highest point of the Applicable script, trailed by a reference to the Library.inc.php library require “DataCleaner.inc.php”; require “Library.inc.php”; You then make a few changes to exploit the irreverence channel, yet after stacking the application into the program, you’re welcomed with the accompanying deadly blunder message: Lethal blunder: Cannot redeclare class Clean

You’re getting this blunder since it’s impractical to utilize two classes of a similar name inside a similar script. Beginning with PHP 5.3, there’s a straightforward approach to determine this issue by utilizing namespaces. You should simply relegate a namespace to each class. To do as such, you have to make one adjustment to each record. Open Library.inc.php and place this line at the top:

namespace Com\Wjgilmore\Library;

Similarly, open DataCleaner.inc.php and place the accompanying line at the top:

namespace Com\Thirdparty\DataCleaner;

You can then start utilizing the separate clean classes without dread of name conflicts. To do as such, instantiate each class by prefixing it with the namespace, as showed in the accompanying case:

<?php

require “Library.inc.php”;

require “Data.inc.php”;

utilize Com\Wjgilmore\Library as WJG;

utilize Com\Thirdparty\DataCleaner as TP;

/Instantiate the Library’s Clean class

$filter = new WJG\Clean();

/Instantiate the DataFilter’sClean class

$profanity = new TP\Clean();

/Create a book title

$title = “the moronic sun likewise rises”;

/Output the title before sifting happens

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

/Remove foulness from the title

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

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

/Remove white space and underwrite title

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

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

?>

Executing this script delivers the accompanying yield: Title before channels: the numbskull sun additionally rises Title after TP\Clean: the shallow sun additionally 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