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.

CHAPTER 7 • ADVANCED OOP FEATURESwww.it-ebooks.infoABSTRACT CLASS OR INTERFACE?When should you use an interface instead of an abstract class, and vice versa? This can be quite confusingand is often a matter of considerable debate. However, there are a few factors that can help you formulatea decision in this regard:If you intend to create a model that will be assumed by a number of closely relatedobjects, use an abstract class. If you intend to create functionality that willsubsequently be embraced by a number of unrelated objects, use an interface.If your object must inherit behavior from a number of sources, use an interface.<strong>PHP</strong> classes can inherit multiple interfaces but cannot extend multiple abstractclasses.If you know that all classes will share a common behavior implementation, use anabstract class and implement the behavior there. You cannot implement behaviorin an interface.Introducing NamespacesAs you continue to create class libraries as well as use third-party class libraries created by otherdevelopers, 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 bookcollection and allows visitors to comment on any books found in your personal library. To manage thisdata, you create a library named Library.inc.php, and within it a class named Clean. This classimplements a variety of general data filters that you could apply to not only book-related data but alsouser comments. Here’s a snippet of the class, including a method named filterTitle() which can beused to clean up both book titles and user comments :class Clean {}function filterTitle($text) {// Trim white space and capitalize first wordreturn ucfirst(trim($text));}Because this is a G-rated Web site, you also want to pass all user-supplied data through a profanityfilter. An online search turned up a <strong>PHP</strong> class library called DataCleaner.inc.php, which unbeknownst toyou includes a class named Clean. This class includes a function named RemoveProfanity(), which isresponsible for substituting bad words with acceptable alternatives. The class looks like this:class Clean {function removeProfanity($text) {172

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

Saved successfully!

Ooh no, something went wrong!