March 1, 2021
Hot Topics:

Implementing Patterns within PHP

  • By W. Jason Gilmore
  • Send Email »
  • More Articles »

Factory Method

Often, a particular application feature can take on a variety of forms. For example, suppose that your Web application offered a letter-writing wizard. Two letter formats are offered: formal and casual. Because its entirely up to the user as to which format is used, instantiation can only take place at runtime. However, how does our system know which class has been instantiated? You might be surprised to know that it doesn't even have to know.

The factory method offers a unified point for instantiating any of several classes, abstracting the process away from the system. These classes implement a certain set of methods which are made known to the system (either via an abstract class or an interface). By forcing this constraint, we can be rest assured that provided an object, the methods executed against it by the system will correspond accordingly. If we later decide to add additional implementations, for example a cover letter and memo format, all we need to do is modify the factory class accordingly. Listing 1-2 implements the letter-wizard application.

Listing 1-2. A PHP-based Factory Method Implementation

<?phpinterface IDocument{   function outputHeader();   function outputBody();   function outputFooter();}class Formal implements IDocument{   public function outputHeader() {      $header = "<p>".date("F d, Y")."</p>";      $header .= "<p>Dear Sir or Madam:</p>";      return $header;   }   public function outputBody($body) {      $body = str_replace(":-)","",$body);      $body = "<p>".$body."</p>";      return $body;   }   public function outputFooter($name) {      return "Sincerely,<br />$name";   }}class Casual implements IDocument {   public function outputHeader() {      return "What's up!<br />";   }   public function outputBody($body) {      $body = "<p>".$body."</p>";      return $body;   }   public function outputFooter($name) {      return "Cya,<br />$name";   }}class DocumentMaker {   function create($type) {      switch($type) {         case 'formal' :            $doctype = new Formal();            break;         case 'casual' :            $doctype = new Casual();            break;         default:            $doctype = new Casual();      }      return $doctype;   }}$docmaker = new DocumentMaker();$docobj = $docmaker->create("formal");echo $docobj->outputHeader();echo $docobj->outputBody("Thank you for lunch today. I appreciate it. :-)");echo $docobj->outputFooter("Jason");?>

Passing "casual" into $docmaker create() method yields:

What's up!Thank you for lunch today. I appreciate it. :-)Cya,Jason

Passing "formal" into the $docmaker create() method yields:

April 15, 2004Dear Sir or Madam:Thank you for lunch today. I appreciate it. Sincerely,Jason


Doing anything cool with PHP and design patterns? Have a question or comment? E-mail me at jason@wjgilmore.com. I'd also like to hear more about your general experience experimenting with PHP 5! Thanks to Mehran Habibi for his valuable input on this article.

About the Author

W. Jason Gilmore (http://www.wjgilmore.com/) is the Open Source Editorial Director for Apress (http://www.apress.com/). He's the author of the upcoming book, PHP 5 and MySQL: Novice to Pro, due out by Apress in 2004. His work has been featured within many of the computing industry's leading publications, including Linux Magazine, O'Reillynet, Devshed, Zend.com, and Webreview. Jason is also the author of A Programmer's Introduction to PHP 4.0 (453pp., Apress). Along with colleague Jon Shoberg, he's co-author of "Out in the Open," a monthly column published within Linux magazine.

Page 3 of 3

This article was originally published on April 26, 2004

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date