11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 7 • ADVANCED OOP FEATURES$badwords = array("idiotic" => "shortsighted","moronic" => "unreasonable","insane" => "illogical");}// Remove bad wordsreturn strtr($text, $badwords);}Eager to begin using the profanity filter, you include the DataCleaner.inc.php file at the top of therelevant script, followed by a reference to the Library.inc.php libraryrequire "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 CleanYou’re receiving this error because it’s not possible to use two classes of the same name within thesame script. Starting with <strong>PHP</strong> 5.3, there’s a simple way to resolve this issue by using namespaces. Allyou need to do is assign a namespace to each class. To do so, you need to make one modification to eachfile. Open Library.inc.php and place this line at the top:namespace Com\Wjgilmore\Library;Likewise, open DataCleaner.inc.php and place the following line at the top:namespace Com\Thirdparty\DataCleaner;You can then begin using the respective Clean classes without fear of name clashes. To do so,instantiate each class by prefixing it with the namespace, as demonstrated in the following example:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!