?– Caution At the time of writing, the namespaces feature had only very recently been integrated into
the PHP 6 development build, and could likely change, perhaps even substantially, between this time
and the PHP 6 release.
To illustrate the challenge, suppose you??™ve created a Web site that enables you to
organize your book collection, and allows visitors to comment on any books found in
your personal library. To manage comments, you??™ve created a library called Library.
inc.php, which includes 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
CHAPTER 7 ?– ADVANCED OOP FEATURES 209
comments. For example, the following shows the class with a filter that capitalizes the
first word of the title comment. This class looks like this:
class Clean {
function FilterTitle($text) {
// Trim white space and capitalize first word
return ucfirst(trim($text));
}
}
However, as this is a G-rated Web site, you 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 {
function RemoveProfanity($text) {
$badwords = array("idiotic" => "shortsighted",
"moronic" => "unreasonable",
"insane" => "illogical");
// Remove bad words
return strtr($text, $badwords);
}
}
Eager to begin using the profanity filter, you include the DataCleaner.
Pages:
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287