php|architect's Guide to PHP Design Patterns 0-9735898-2-5

Author Jason E. Sweat's book php|architect's Guide to PHP Design Patterns is the first, comprehensive guide to

335 58 3MB

English Pages 337 Year 2005

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
php|architect's Guide to PHP Design Patterns......Page 1
Table of Contents......Page 7
1. Programming Practices......Page 23
2. The Value Object Pattern......Page 37
3. The Factory Pattern......Page 51
4. The Singleton Pattern......Page 73
5. The Registry Pattern......Page 83
6. The MockObject Pattern......Page 99
7. The Strategy Pattern......Page 121
8. The Iterator Pattern......Page 135
9. The Observer Pattern......Page 159
10. The Specification Pattern......Page 171
11. The Proxy Pattern......Page 189
12. The Decorator Pattern......Page 201
13. The Adapter Pattern......Page 217
14. The Active Record Pattern......Page 225
15. The Table Data Gateway Pattern......Page 245
16. The Data Mapper Pattern......Page 259
17. The Model-View-Controller Pattern......Page 281
18. Conclusion......Page 297
A. Pattern Quick Reference......Page 301
B. SimpleTest Testing Practices......Page 311
Index......Page 329
Live PHP Training from php|architect!......Page 337
Recommend Papers

php|architect's Guide to PHP Design Patterns
 0-9735898-2-5

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Design฀patterns฀are฀comprehensive,฀well-tested฀solutions฀to฀common฀problems that฀developers฀everywhere฀encounter฀each฀day.฀Although฀designed฀for฀solving general฀programming฀issues,฀some฀of฀them฀have฀been฀successfully฀adapted฀to the฀specific฀needs฀of฀Web฀development. php|architect’s฀Guide฀to฀PHP฀Design฀Patterns฀is฀the฀first฀comprehensive฀guide฀ to฀the฀application฀of฀design฀patterns฀to฀the฀PHP฀development฀language. Designed฀to฀satisfy฀the฀need฀of฀enterprise-strength฀development,฀you฀will฀find฀ this฀book฀both฀an฀excellent฀way฀to฀learn฀about฀design฀pattern฀and฀an฀ irreplaceable฀reference฀for฀your฀day-to-day฀programming With฀coverage฀of฀more฀than฀XXX฀different฀types฀of฀patterns,฀including฀BLAH, BLAH,฀BLAH,฀BLAH฀and฀much฀more,฀this฀book฀is฀the฀ideal฀resource฀for฀your enterprise฀development฀with฀PHP฀4฀and฀PHP฀5.

NanoBooks฀are฀excellent,฀in-depth฀resources฀created฀by฀the฀publishers฀of฀ php|architect฀(http://www.phparch.com),฀the฀world’s฀premier฀magazine฀dedicated฀ to฀PHP฀professionals.

php|architect’s฀Guide฀to฀PHP฀Design฀Patterns

php|architect’s Guide฀to PHP฀Design฀Patterns

php|architect’s Guide฀to PHP฀Design฀Patterns A฀Practical฀Approach฀to฀Design฀Patterns for฀the฀PHP฀4฀and฀PHP฀5฀Developer Jason฀E.฀Sweat

USA $21.99 Canada $29.99 U.K. £16.99 Net

From฀the฀publishers฀of

Shelve฀under฀PHP/Web฀Development/Internet฀Programming

7.50 x 9.25

Jason฀E.฀Sweat

NanoBooks฀focus฀on฀delivering฀high-quality฀content฀with฀in-depth฀analysis฀and expertise,฀centered฀around฀a฀single,฀well-defined฀topic฀and฀without฀any฀of฀the฀fluff of฀larger,฀more฀expensive฀books.

.309

7.50 x 9.25

PHP|ARCHITECT’S

GUIDE

TO

PHP DESIGN PATTERNS by Jason E. Sweat

php|architect’s Guide to PHP Design Patterns Contents Copyright © 2004-2005 Jason E. Sweat - All Right Reserved Book and cover layout, design and text Copyright © 2004-2005 Marco Tabini & Associates, Inc. - All Rights Reserved

First Edition: July 2005 ISBN 0-9735898-2-5 Produced in Canada Printed in the United States No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical reviews or articles.

Disclaimer Although every effort has been made in the preparation of this book to ensure the accuracy of the information contained therein, this book is provided "as-is" and the publisher, the author(s), their distributors and retailers, as well as all affiliated, related or subsidiary parties take no responsibility for any inaccuracy and any and all damages caused, either directly or indirectly, by the use of such information. We have endeavoured to properly provide trademark information on all companies and products mentioned in this book by the appropriate use of capitals. However, we cannot guarantee the accuracy of such information. Marco Tabini & Associates, The MTA logo, php|architect, the php|architect logo, NanoBook and NanoBook logo are trademarks or registered trademarks of Marco Tabini & Associates Inc.

Bulk Copies Marco Tabini & Associates, Inc. offers trade discounts on purchases of ten or more copies of this book. For more information, please contact our sales offices at the address or numbers below.

Credits Written by

Jason E. Sweat

Published by

Marco Tabini & Associates, Inc. 28 Bombay Ave. Toronto, ON M3H 1B7 Canada (416) 630-6202 (877) 630-6202 toll free within North America [email protected] / www.phparch.com

Edited By

Martin Streicher

Technical Reviewer

Marcus Baker

Layout and Design

Arbi Arzoumani

Managing Editor

Emanuela Corso

Biography Jason E. Sweat Jason graduated from Colorado State University in 1992 as a University Honor Scholar with a Bachelors of Science in Business Administration, concentrations in Computer Information Systems and Finance & Real Estate, and a minor in Mathematics. He spent seven years working for a small engineering firm doing process control work in the steel industry. This let to extensive SQL development and Jason's first web development experience creating ASP pages. He changed employers and worked as a Senior Project Leader for a Fortune 100 industrial manufacturer, leading a team of developers for commercial applications, and acting as the web master for his business unit. His role changed again in January 2005, and Jason is now the Manager of eBusiness/Commercial Systems for the same business unit. Jason has used PHP since 2001, where he was searching for a free‹as in beer ;) ‹substitute for IIS/ASP to create an accounting system for a home business. His Unix administrator pointed him towards Linux, Apache and PHP. He has since adopted PHP as an intranet development standard at work, as well as using PHP in a Unix shell scripting environment. He was a co-author of PHP Graphics Handbook (Wrox 2003), has published several articles for the Zend website and for php|architect magazine, and has presented numerous talks on PHP at various conferences. Jason is a Zend Certified Engineer, and maintains a blog at http://blog.casey-sweat.us/. Jason currently resides in Iowa with his wife and two children. He enjoys many activities with his family including camping, hiking and swimming. He also enjoys practicing the Japanese martial art of Aikido.

To my wife, Vicki, and to my children, Madeline and Caleb, for putting up with “even more” time with Daddy on the computer. Thank you for your support and love.

CONTENTS Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 The Goal of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Assumed Reader Skill Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 PHP4 and PHP5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Object Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Additional Resources and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22

1 Programming Practices . . . . . . . . . . . . . . . . . . . . . .25 Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 Refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Other Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 UML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Source Code Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35

2 The Value Object Pattern . . . . . . . . . . . . . . . . . . . . .39 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 PHP 5 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 In Context Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 PHP 4 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Business Logic in ValueObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49

3 The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . .53 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53

10

Table of Contents

The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Adding a Little Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Factories to Hide Object State Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 Factories to Promote Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Factories for Lazy Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72

4 The Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . .75 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 A “Global” Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 A Static Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 The Singleton in PHP5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 The Monostate Pattern: Stealth Singletons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81

5 The Registry Pattern . . . . . . . . . . . . . . . . . . . . . . . . .85 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .85 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .90 Implementing the Registry as a MonoState Object . . . . . . . . . . . . . . . . . . . . . . .92 Implementing with Class Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Embedded Registry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98

6 The MockObject Pattern . . . . . . . . . . . . . . . . . . . . .101 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 A Legacy Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .119 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120

Table of Contents

7 The Strategy Pattern . . . . . . . . . . . . . . . . . . . . . . . .123 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 An Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 Related Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134

8 The Iterator Pattern . . . . . . . . . . . . . . . . . . . . . . . . .137 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 A Variant Iterator API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Filtering Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .150 Sorting Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 SPL Iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158

9 The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . .161 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170

10 The Specification Pattern . . . . . . . . . . . . . . . . . . .173 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Traveling to Warm Destinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 Parameterized Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178

11 The Proxy Pattern . . . . . . . . . . . . . . . . . . . . . . . . . .191 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194

11

12

Table of Contents

RemoteProxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194 Lazy Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198 Dynamic Proxy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201

12 The Decorator Pattern . . . . . . . . . . . . . . . . . . . . .203 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216

13 The Adapter Pattern . . . . . . . . . . . . . . . . . . . . . . .219 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224

14 The Active Record Pattern . . . . . . . . . . . . . . . . . .227 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228 Test Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .230 Record Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 Testing Database Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236 Active Record Instance ID . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .237 Searching for Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .238 Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .243

15 The Table Data Gateway Pattern . . . . . . . . . . . .247 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 Test Case Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249 Returning Recordsets as Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250

Table of Contents

Returning Iterable Object Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252 Updating Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .258

16 The Data Mapper Pattern . . . . . . . . . . . . . . . . . . .261 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262 Sample Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263 Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .281

17 The Model-View-Controller Pattern . . . . . . . . .283 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 The Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .283 The Model-View-Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .284 The Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 Domain Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 The View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286 Template View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .286 The Transform View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290 The Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291 Front Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291 Application Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 Cross-Cutting MVC Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295 Non-MVC Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Inversion of Control Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296

18 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 A Pattern Quick Reference . . . . . . . . . . . . . . . . . . . .303 Book References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310

B SimpleTest Testing Practices . . . . . . . . . . . . . . . . .313 Best Practices for Using SimpleTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314

13

14

Table of Contents

Mock Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .317 Web Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319 Our Legacy Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .320 Partial Mock Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .325

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .331

Introduction

H

AVE YOU EVER STARTED to tackle a new feature in your application only to realize that its solution is strikingly similar to something that you’ve already implemented? If you’ve been a programmer for even a short time, the answer is probably “Yes” and it’s likely that you’ll reach for some existing code to bootstrap your new development. You might even realize that your solution is fundamental, an approach that can be applied widely and repeatedly, not just by you, but by all professional developers. In fact, many programming problems are faced over and over again, and many fundamental solutions—or design patterns—have emerged to address them. Design patterns are a template for how to organize your code so you can take advantage of a tried-and-true design.

18



Introduction

Design Pattern History The term “design pattern” was originally coined in the field of architecture. Christopher Alexander, in his 1977 work, “A Pattern Language: Towns/Building/Construction,” describes common issues of architectural design and explains how new, effective designs can be created through the aggregation of existing, well-known patterns. Alexander’s concepts translate well into software development, where it’s long been desirable to construct solutions from previously existing components.

All design patterns have several common characteristics: a name, a problem statement, and a solution. • The name of a design pattern is important, because it allows you to instantly communicate the intent of your code with other programmers—at least programmers familiar with patterns—without going into too much detail. • The problem is the domain where the pattern can be applied. • The solution describes the implementation of the pattern. Good coverage of a pattern should discuss the pros and cons of the pattern’s use. A pattern is a useful technique to solve a given problem. A design pattern isn’t a library—code to be included and used directly in your project—but rather a template for how your code can be structured. Indeed, a code library and a design pattern are applied much differently. For example, a shirt you buy off the rack at a department store is a code library. Its color, style, and size were determined by the designer and manufacturer, but it meets your needs. However, if nothing in the store suits you, you can create your own shirt—designing its form, choosing a fabric, and stitching it together. But unless you are a tailor, you may find it easier to simply find and follow an appropriate pattern. Using a pattern, you get an expertly-designed shirt in far less time. Returning the discussion to software, a database abstraction later or a content management system is a library—it’s pre-designed and already coded, and a good choice if it meets your requirements exactly. But if you’re reading this book, chances are that off-the-shelf solutions don’t always work for you. Yet you know what you want and are capable of realizing it; you just need a pattern to guide you. One last thought: like a sewing pattern, a design is of little use on its own. After all, you can’t wear a pattern—it’s just a patchwork of thin paper. Similarly, a software design pattern is just a guide. It must still be tailored specifically to a programming language and your application’s features and requirements.

Introduction

The Goal of This Book The goal of this book is not to present a comprehensive catalog of software design patterns or to develop any new design patterns or terminology, but rather to highlight a few of the existing, wellknown design patterns. In particular, the book presents those patterns that I’ve found most useful for development of dynamic web applications and shows reference implementations for these patterns in PHP.

Object Oriented Programming By the very nature of design patterns, a good deal of this book is based on the concepts and practices of Object Oriented Programming (OOP). If you’re not familiar with OOP, there are many resources—books, web sites, magazines, and classes—to help you learn more about it. Much of the OOP materials extol the benefits of code reuse, robustness, encapsulation, polymorphism, and extensibility, each of which is important and valuable. However, I believe the main benefit of OOP is how it encourages you to distill the problem at hand into manageable pieces. Designed and implemented in focused, small pieces, your code can be tested more thoroughly and is easier to understand and maintain.

Assumed Reader Skill Set This book assumes that you’re already fluent with PHP. In particular, it presupposes that you have a working knowledge of PHP and its syntax and understand the fundamentals of PHP’s implementation of OOP. This book isn’t intended to be an introduction to PHP programming, nor to OOP in PHP. Because not all practitioners of OOP use the same terminology, where new terminology is introduced, it’s defined in the text or in a sidebar.

PHP4 and PHP5 As I write this book, PHP5 has been released for some time but has yet to be widely adopted in the hosting community. In my own job, I’ve started to migrate new development of applications to PHP 5.0.3 and am very pleased so far with both its backwards compatibility with PHP4 code and its new object model, which is one of the significant new features of PHP5 and the main driver for my adoption. There are many fine articles and tutorials dealing with the nuances of the change in the object model between PHP versions, but the short story is that PHP5 offers: • Object handles (explained below, and further in Chapter 2: The Value Object Pattern) • Better constructors (uniform name, changing $this not allowed)

19

20

Introduction

• Destructors now exist • Visibility (public, protected, private for methods and attributes) • Exceptions (an alternative to triggering errors using the new try{} catch{} syntax) • Class constants (defines using the class for a name space) • Reflection (dynamic examination of classes, methods and arguments) • Type hinting (specifying expected classes or interfaces for method arguments) PHP5 also offers a few more obscure features: • New magic methods (__get() and __set() allow you to control attribute access; __call() lets you dynamically intercept all method calls to the object; __sleep() and __wakeup() let you override serialization behavior; and __toString() lets you control how an object represents itself when cast as a string) • Autoloading (allows the end user to try to automatically load the class the first time a reference to it is made) • Final (do not allow a method or a class to be overridden by subclasses)

Object Handles The best news in PHP5 is all objects are now defined by handles, similar to a system resource like a file or a database handle. Passing an object to a PHP function no longer implicitly makes a copy of the object. To see the difference, consider the following two examples:

// PHP4 class class ExampleP1 { var $foo; function setFoo($foo) { $this->foo = $foo`; } function getFoo() { return $this->foo; } } function changeExample($param) { $param->setFoo(‘blah’); return $param->getFoo(); } $obj = new ExampleP1; $obj->setFoo(‘bar’); echo $obj->getFoo(); // bar echo ChangeExample($obj); //blah echo $obj->getFoo(); // bar

Introduction

In PHP4, the variable $param in changeExample() contains a copy of $obj. So, the function doesn’t alter the value of $foo in the original object and the final $obj->getFoo() prints “bar.” In PHP5, because $obj is passed as a handle, the same changeExample() function does effect the original object. In other words, using handles, a copy isn’t made and $param is the instance $obj.

// PHP5 class class ExampleP2 { protected $foo; function setFoo($foo) { $this->foo = $foo; } function getFoo() { return $this->foo; } } $obj = new ExampleP2; $obj->setFoo(‘bar’); echo $obj->getFoo(); // bar echo ChangeExample($obj); //blah echo $obj->getFoo(); // IMPORTANT, produces blah

This issue becomes even more complicated when you pass the $this variable to other objects or functions inside of the object constructor. What this boils down to is that in PHP4 you need to (nearly) always: • Create an object by reference, as in $obj =& new Class; • Pass an object by reference, like function funct(&$obj_param) {} • Catch an object by reference function &some_funct() {} $returned_obj =& some_funct() Now, there are some cases where you actually want to have a copy of the original object. In my PHP4 code, I always comment any non-reference assignment of an object as an intentional copy. In the long run, such a brief comment can save you or anyone else maintaining your code a great deal of headaches. Reference passing, object handles, and object copies are explored in greater detail in Chapter 2, “The Value Object Pattern.” Despite my personal preference to move towards PHP5 development, my feeling is that PHP4 will continue to be with us for quite some time and existing public projects should continue to support it. To that end, this book tries to provide equal footing to both versions of PHP. Whenever possible, both PHP4 and PHP5 versions of example code are provided and explained. Within each chapter, each code block that changes from one version of PHP to another has a comment of // PHP4 or

21

22

Introduction

// PHP5 to indicate the change. Subsequent blocks of code are in the same version of PHP, until the next switch is indicated.

Additional Resources and References There are a number of great references available to help you learn more about design patterns. The “bible” of design patterns is Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (his seminal work is often referred to as the “Gang of Four” or simply “GoF,” in reference to the four authors). Throughout this book, the GoF names of patterns are used as the canonical source. Following “Design Patterns,” the next most useful book on design patterns for PHP web application developers is Patterns of Enterprise Application Architecture by Martin Fowler. Fowler’s book details many patterns that are of use specifically in the task of developing web application, in contrast with the broader coverage of general patterns in GoF. The Web offers many good resources for information on design patterns. One particular standout is the Portland Pattern Repository at http://c2.com/ppr/. A good site for reference patterns implemented in PHP is ::phpPatterns(), located online at http://www.phppatterns.com/.

Acknowledgments I would like to thank my employer, where my role and responsibilities allow me to spend a portion of my time in this area I love, providing me with the knowledge and experience to have the confidence to write this book. Another source of inspiration, ideas, and experience is the SitePoint (http://www.sitepoint.com/) forums. In particular, the regular contributors to the “Advanced PHP Forum” have a great wealth of experience and knowledge, which they regularly share in one of the most generous and helpful communities I’ve found on the Internet. It was through this resource I located SimpleTest (http://simpletest.sf.net/), WACT (http://wact.sf.net/) and numerous other PHP projects that I’ve found invaluable. I hope SitePoint continues to be a great resource for PHP developers for many years to come. This book clearly could not have come into existence without the significant efforts and dedication of the PHP team, who developed a useful, easy to learn, and versatile language that’s very wellsuited to the ecological niche of web applications. Finally, I’d like to thank Marco Tabini and the staff of php|architect. The magazine has been a source of many varied PHP topics, presented by professional developers with extensive knowledge to share. The conferences organized by Marco and company have been great as well.

1 Programming Practices

L

EARNING A NEW TECHNIQUE means adopting new practices. This chapter introduces, or perhaps reinforces, several practices that you’ll likely find very useful as you implement design patterns in your code. Many of the practices summarized here are worthy of an individual chapter, even an entire book. You should consider this chapter an introduction to pattern-related practices with a PHP spin and look at the references listed throughout to investigate a topic further.

Testing Your Code Probably no other coding practice is as important as testing your code. With good testing comes great freedom. At first, that “motto” might strike you as counter-intuitive. If anything, you might assert, testing seems an impediment to freedom. To the contrary: if you can run tests that completely exercise your software’s public interface, you can change the internals of your implementation without changing (or

26

Programming Practices

worse, breaking) existing applications. Testing validates the veracity and accuracy of your published interface, letting you readily change the inner workings of your code with complete confidence that it remains accurate and bug-free — that you’ve not introduced new bugs or reintroduced old bugs. Before talking more about the benefits of testing, let’s look at an example. All of the tests in this book use the SimpleTest PHP testing framework, available at http://simpletest.org/. Consider this code:



place for the page title

sample title

  • color




  • Of course, the point of all of these solutions is to separate the presentation of Model data from the Model and from the application itself. Each of the prior examples produced essentially the same content, so the selection of which to use is largely a matter of personal preference.

    The Transform View The Transform View extracts data from your model and transforms the data into the desired output format. It essentially amounts to using a language to step through the elements of your data one by one, assembling the output along the way. The difference between the Template View and the Transform View is the direction of data flow. In the Template View you start with a skeleton of your output and insert domain data into it. With the Transform View you start with the data and build the output from it.

    The Model-View-Controller Pattern

    The dominant technology for implementing a Transform View is XSLT.

    The Controller The Controller is the one role of MVC that most PHP MVC frameworks address. This is reasonable considering that Models are specific to the application and nearly every developer already has their favorite template engine, a major component of the View. That leaves interpreting the HTTP response, and controlling application flow (selecting the appropriate action to take or view to display), both approachable tasks for a generic framework.

    Front Controllers It’s often helpful to centralize the control of application flow at a single point. Centralization can help you understand how a complex system operates and it also provides a single place where you can insert global code such as an Intercepting Filter pattern. A Front Controller is perfect for centralization.



    Intercepting Filter The Intercepting Filter pattern is an implementation of the Chain of Responsibility pattern from the GoF book. It allows for sequential processing of a request to apply common tasks such as logging or security. There are two common implementations, one where the filters are applied sequentially in a chain until the application controller is reached, and another that resembles a series of decorators, useful for performing both pre- and post-filter actions (think of a whitespace removal or a compressing filter where you might start output buffering in pre-processing and perform your filter in the post-processing action).

    As a simple example of what an Intercepting Filter might look like integrated with a Front Controller, assume we have in interface for our Filters which has both preFilter() and postFilter() methods. We can then build a means of adding filters to our FrontController:

    class FrontController { var $_filter_chain = array(); function registerFilter(&$filter) { $this->_filter_chain[] =& $filter; } }

    And then we can apply the preFilter() methods in sequence, prior to running the actual work of the FrontController (page generation, dispatching, etc). After the FrontController has performed

    291

    292

    The Model-View-Controller Pattern

    its task, the postFilter() methods could be called in reverse order.

    class FrontController { //... function run() { foreach(array_keys($this->_filter_chain) as $filter) { $this->_filter_chain[$filter]->preFilter(); } $this->_process(); foreach( array_reverse(array_keys($this->_filter_chain)) as $filter) { $this->_filter_chain[$filter]->postFilter(); } } function _process() { // do the FrontController work } }

    As an example, this HtmlCommentFilter class would remove all HTML comments from the resulting output of the page.

    class HtmlCommentFilter { function preFilter() { ob_start(); } function postFilter() { $page = ob_get_clean(); echo preg_replace( ‘~~ims’ ,’’ ,$page); } }

    Application Controllers Front Controllers often delegate control to an Application Controller and the Application Controller pattern is really the heart of what the MVC Controller is all about. The primary responsibility of the Controller is deciding what the application should do in response to an incoming request. A typical way of implementing a Controller is using the Command pattern. The Command pat-

    The Model-View-Controller Pattern

    tern encapsulates an action in an object so you can parameterize a request, queue it, log it, or support operations like undoing an action. In the context of a web application, they are useful as the target of code that dispatches to a concrete Command to carry out the work of a particular HTTP request. Essentially, the Command pattern lets you break down the discrete behaviors of your application and code, each as a small, manageable class, with a uniform API to allow the Controller to dispatch to a specific concrete Command to implement the desired application functionality. Don’t let this buzzword-laden talk of controllers and dispatching confuse you. If you’ve spent even a few hours with PHP, you’ve likely written some kind of an Application Controller. For example, a simple form that posts back to itself, such as...

    if (count($_POST)) { // do form handling code } else { // display the form }

    ... is a form of Application Controller. A somewhat more complex Application Controller is something like this:

    switch ($_POST[‘action’]) { case ‘del’: $action_class = ‘DeleteBookmark’; break; case ‘upd’: $action_class = ‘UpdateBookmark’; break; case ‘add’: $action_class = ‘InsertBookmark’; break; case ‘show’: default: $action_class = ‘DisplayBookmark’; } if (!class_defined($action)) { require_once ‘actions/’.$action_class.’.php’; } $action =& new $action_class; $action->run();

    Another possible way to implement dispatching is to have a configuration that loads an associative array. You might end up with:

    $action_map = array( ‘del’ => ‘DeleteBookmark’

    293

    294

    The Model-View-Controller Pattern

    ,’upd’ => ‘UpdateBookmark’ ,’add’ => ‘InsertBookmark’ ); $action_class = (array_key_exists($_POST[‘action’], $action_map)) ? $action_map[$_POST[‘action’]] : ‘DisplayBookmark’; if (!class_defined($action)) { require_once ‘actions/’.$action_class.’.php’; } $action =& new $action_class; $action->run();

    My experience with web applications has shown that a “double dispatching” architecture can be a useful mental map to compare frameworks’ dispatching mechanisms against. The first dispatch is to an “action,” any event that needs to perform an action using your Model. After any visible action, an HTTP redirect would be issued to instruct the client to fetch a particular View. The second dispatch is to select a specific View. (In early procedural incarnations of this methodology, I used a case statement, but the MVC paradigm lends itself to using the Command pattern to perform this dispatch.) The “real life” version of a Model-View-Controller sequence diagram looks fairly similar to the “ideal” sequence diagram shown above. The main addition is an ActionFactory to produce each Action, which is a concrete Command.

    In many of the MVC implementations I have developed, the second dispatch is performed by the default ShowViewAction.

    The Model-View-Controller Pattern

    This diagram shows the first dispatch creating the concrete Command ShowViewAction. This action would in turn use a ViewFactory to create a concrete View class, which is an example of what Martin Fowler calls a View Helper in the PoEAA section on Views in MVC. This View would use your preferred TemplateEngine to select and parse a template file, populate template variable: from data in the Model, and render the resulting content from the template and return it to the client. It is this kind of a diagram that can give MVC a reputation for bloat, but in fact, each element of this diagram was added in response to a need to organize the code to make it easier to maintain. In general, I have found the most significant hurdle to using a specific framework is gaining an understanding of how that framework operates and how to add application-specific features. The actual organization is typically straightforward once understood, but it seems at first daunting and unapproachable with no context to work from.

    Cross-Cutting MVC Concerns There seems to be a number of “what goes where” questions surrounding MVC, and you can receive substantially different answers from different MVC proponents. Where does $_SESSION belong? One argument says that sessions are a persistent data store, usually implemented as files on the server and are therefore best kept in the Model. A second set of developers argues that like the other PHP superglobals, session data is an input to the system and therefore belongs in the Controller. Yet another set of developers say sessions are implemented using cookies, a technology that only works with HTML over HTTP and therefore sessions are View related. Where does authentication belong? It seems like it’s part of application logic and would therefore belong to the Model. But what if you want to limit certain actions (part of the Controller) to only

    295

    296

    The Model-View-Controller Pattern

    authenticated users? Well, the Controller can access the Model, so it seems a perfect place. But what about HTTP authentication? Does it too go into the Controller? Where does the browser fit in this whole concept? Clearly the View, right? What if you try to implement Javascript validation? Doesn’t validation belong in the Controller and the Model? How do you get it into the View? None of these issues are show stoppers, but each can cause some thought provoking, even gutwrenching moments when trying to figure out just how to align these concerns in your MVC implementation.

    Non-MVC Frameworks Clearly not every framework is centered on the separation of concerns and ideas embodied by the MVC pattern. Here is a small sampling of non-MVC framework ideas.

    Event Handling When you work in a GUI environment, the tools are generally set up to responsd to events. Think button.click(). Several PHP frameworks have tried to adopt this as a core idea. Prado was recently recognized in Zend’s PHP5 coding contest and has event handling as a core concept. WACT has the concept of using the Composite pattern to aggregate controllers, each of which has “listeners” that can approximate an event handling perspective.

    Inversion of Control Containers A hot topic in Java circles is Inversion of Control (IoC) Containers, also known as the Dependency Injection pattern. A good introductory article on this pattern is available at http://www.martinfowler.com/articles/injection.html. There is a promising PHP5 project that’s a port of the original Java PicoContainer at http://www.picocontainer.org/. Dependency Injection is a pattern I personally am very interested in using in my own development efforts because it inherently works well with the Test Driven Development methodology, allowing you to more readily test your code because it is designed to play nicely with other components right from the start. This pattern is really orthogonal to MVC—one of the areas I am most interested in is combining a Dependency Injection container like Pico and a MVC framework like WACT to produce an application that “autowires” itself. Ideally this will create easy to assemble web applications, and at the same time allow for easily testable code by instructing the container to inject Mock Objects instead of real dependencies.

    The Model-View-Controller Pattern

    Conclusion This has been a whirlwind tour of MVC and of related design patterns. If you want to look at fully developed PHP MVC frameworks, I would recommend reviewing Mojavi (http://www.mojavi.org/); it’s a good example of the pattern and the project has active development and a robust community. As you should know by now, I am partial to WACT (http://phpwact.org/), which has the distinction of having framework components for all three parts of the MVC triad: a Composite Controller mechanism, a Custom Tag template system for Views, and the DataSource (see Chapter 10 — The Specification Pattern) as a generic proxy for the Model. While this chapter may not have solved any web architecture problems you have, hopefully it has provided you with some ideas—starting points for further research—and perhaps even the inspiration for you to write the Magic Web Application Architecture that revolutionizes PHP development. If you do, please make sure to let the rest of us know.

    297

    18 Conclusion

    W

    E HAVE COME TO the end of our brief journey together. It is my hope we have accomplished something together, both the overt goal of introducing the concepts of Design Patterns with specific examples in PHP, but also more subtle accomplishments, such as the introduction of Agile Development techniques like Test Driven Development. Design Patterns are a useful tool for you to have in your programming toolkit, to be pulled out and applied appropriately to complicated design problems. Design patterns give you the possibility to quickly add flex points to your design, using time-worn, proven techniques. Using Design Patterns also gives you an additional ability to communicate with other developers, with the names of the each patterns now acting as a short cut for a whole body of knowledge. It is possible to go overboard with any technique, including OOP or Design Patterns. This is where it’s important to apply other Agile Development practices, specifically “Do the simplest possible thing that will work.” If the task at hand doesn’t require the flexibility or warrant the complexity of the Design Pattern based solution, then don’t use it. Design Patterns are mean to solve specific problems

    300

    Conclusion

    in your code, not to create problems. Testing your code is incredibly powerful. There has been clear evidence of this throughout the book, with each chapter heavily emphasizing the testing of the patterns as well as the coding of the pattern implementation itself. No other practice has influenced the design, stability, and maintainability of the code I have written as much as adopting the practice of testing. The code in this book was developed by first writing the unit test cases and the code, and only when the code passed the tests did I migrate it into the body of each chapter. The full source code for each of these tests is available for you in the source code download for the book. I encourage you to review these tests, understand them, even see their shortcomings. But most of all I encourage you to take the step of testing your own code if you’re not doing so now. The Design Patterns presented in this book are by no means comprehensive, but were instead intended to provide you with a sample of some of the most commonly applicable patterns in the PHP/Web Application context. This book is a starting point for you begin your own journey of understanding and application of Design Patterns. One of my personal goals in writing this text was to gain better clarity and understanding of the patterns myself, under the assumption there is no better way to learn than to teach. I believe I have achieved this goal, and I hope you find as much utility in reading this work as I had in authoring it. I wish you the best in applying Design Patterns to your PHP applications. Happy coding.

    A Pattern Quick Reference

    O

    ne of the downsides of design patterns is that there are so many of them—this book covers almost twenty of them in great detail, and there are several more that are not directly relevant to PHP. Therefore, I thought that it might have been handy to have a simple (and, thankfully, relatively short) reference table that can help you jog your memory when you're looking for a pattern to solve a particular problem but can't quite remember which pattern you should use. The table in the following pages provides you with a short overview of every pattern covered in this book, together with the chapter in which it is covered and pointers to external resources like books. Together with the index, it will hopefully provide you with a quick way to locate the pattern for every occasion.

    304

    Pattern Quick Reference

    A

    Application Controller

    bstractFactory

    Summary

    Facilitates the building of families of related objects.

    Reference Chapter

    3

    Other Resources

    GoF—Page 87

    ActiveRecord Summary

    Creates an object that wraps a row from a database table or view, provides database access one row at a time, and encapsulates relevant business logic.

    Reference Chapter

    14

    Other Resources

    PoEAA—Page 160 Data Access Patterns—Page 33

    Summary

    A central point for handling navigation for an application, typically implemented in an index.php file dispatching based on URL query parameters.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page 379

    B

    uilder

    Summary

    Facilitates the initialization of complex object state.

    Reference Chapter

    3

    Other Resources

    GoF—Page 97

    Adapter Allow classes to support a familiar interface so you can use new classes without refactoring old code.

    C

    Reference Chapter

    13

    Summary

    Encapsulate a request as an object.

    Other Resources

    GoF—Page 139 Agile Software Development—Page 317 Roles, Responsibilities and Collaborations—Page 340 Design Patterns Explained—Page 95 Advanced PHP Programming—Page 44

    Reference Chapter

    17

    Other Resources

    GoF—Page 233

    Summary

    ommand

    Pattern Quick Reference

    Composite

    Decorator

    Summary

    Manage a collection of objects where each "part" can stand in as a "whole". Typically organized in a tree hierarchy.

    Summary

    Attach responsibilities to an object dynamically. Can simplify class hierarchies by replacing subclasses.

    Reference Chapter

    10, 17

    Reference Chapter

    12

    Other Resources

    GoF—Page 163 Agile Software Development—Page 293

    Other Resources

    GoF—Page 175 Design Patterns Explained—Page 241

    Custom Tag Summary

    Improve presentation separation by encapsulating components to appear as new HTML tags.

    Reference Chapter

    17

    Other Resources

    GoF—Page 139 PoEAA—Page 374 http://wact.sf.net/index.php/TemplateView

    D

    ataMapper

    Summary

    An object that acts as a translation layer between domain objects and the database table that contains related data.

    Reference Chapter

    16

    Other Resources

    PoEAA—Page 165 Data Access Patterns—Page 53

    Dependency Injection Summary

    Construct classes to accept collaborators through the constructor or setter methods, so that a framework can assemble your objects.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page ??? http://www.martinfowler.com/articles/injection.ht ml

    Domain Model Summary

    An object model of business logic that includes both data and behavior.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page 116 Evans DDD

    305

    306

    Pattern Quick Reference

    F

    actoryMethod

    Summary

    Facilitates the creation of objects.

    Reference Chapter

    3

    Other Resources

    GoF—Page 107 Agile Software Development—Page 269 Design Patterns Explained—Page 285 Advanced PHP Programming—Page 54

    Front Controller Summary

    A controller that handles all requests for a web application.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page 379

    I

    terator

    Summary

    Easily manipulate collections of objects.

    Reference Chapter

    8

    Other Resources

    GoF—Page 257

    M

    ockObject

    Summary

    Supplies a stub that validates whether certain methods were or were not called during testing.

    Reference Chapter

    6, Appendix B

    Other Resources

    http://www.lastcraft.com/mock_objects_documentation.php http://www.mockobjects.com/MocksObjectsPaper.ht ml

    H

    andle-Body

    Summary

    A collective name for design patterns that hold a reference to a subject object (for example, Proxy, Decorator, and Adapter).

    Model-View-Controller Summary

    An application layering pattern that separates concerns between your domain model, presentation logic and application flow.

    Reference Chapter

    N/A

    Reference Chapter

    17

    Other Resources

    http://www.c2.com/cgi/wiki?HandleBodyPattern

    Other Resources

    PoEAA—Page 330 http://wact.sf.net/index.php/ModelViewController

    Pattern Quick Reference

    MonoState Summary

    Allow all instances of an object to share the same state.

    Reference Chapter

    4

    Other Resources

    Agile Software Development—Page 177 http://c2.com/cgi/wiki?MonostatePattern

    O

    bserver

    Summary

    Register objects for later callback. Event-based notification. Publish/Subscribe.

    Reference Chapter

    9

    Other Resources

    GoF—Page 293 Agile Software Development—Page 297 Design Patterns Explained—Page 263

    P

    egistry

    Summary

    Manages references to objects through a single, well-known, object.

    Reference Chapter

    5

    Other Resources

    PoEAA—Page 480

    S

    erverStub

    Summary

    Simulates a portion of your application for testing purposes.

    Reference Chapter

    6

    Other Resources

    PoEAA—Page 504

    Singleton

    roxy

    Summary

    R

    Provide access to an object through a surrogate object to allow for delayed instantiation or protection of subject methods.

    Reference Chapter

    11

    Other Resources

    GoF—Page 207 PoEAA—Page 200 Agile Software Development—Page 327

    Summary

    Provide global access to a single instance of an object.

    Reference Chapter

    4

    Other Resources

    GoF—Page 127 Agile Software Development—Page 177 Design Patterns Explained—Page 255 Advanced PHP Programming—Page 56

    307

    308

    Pattern Quick Reference

    Specification Summary

    Flexible evaluation of objects against dynamic criteria.

    Reference Chapter

    10

    Other Resources

    Evans DDD—Page 224, 273

    State

    T

    ableDataGateway

    Summary

    An object that acts as a gateway to a database ta ble or view, providing provide access to multiple rows.

    Reference Chapter

    15

    Other Resources

    PoEAA—Page 165

    Template View

    Summary

    Have an object change its behavior depending on state changes.

    Reference Chapter

    7, 8

    Other Resources

    GoF—Page 305

    Strategy Summary

    Allows for switching between a selection of algorithms by creating objects with identical interfaces.

    Reference Chapter

    6

    Other Resources

    GoF—Page 315 Agile Software Development—Page 161 Roles, Responsibilities and Collaborations—Page 338 Design Patterns Explained—Page 229

    Summary

    Render a page by replacing embedded markers with domain data.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page 361

    TemplateMethod Summary

    Define an algorithm with "hook" methods allowing subclasses to change the behavior without changing the structure.

    Reference Chapter

    7,12

    Other Resources

    GoF—Page 325 Roles, Responsibilities and Collaborations—Page 330 Design Patterns Explained—Page 279 Advanced PHP Programming—Page 49

    Pattern Quick Reference

    Transform View

    Visitor

    Summary

    Process domain data sequentially to transform it to some form of output.

    Summary

    Defines an algorithm as an object that "visits" each member of a aggregate performing an operation.

    Reference Chapter

    17

    Reference Chapter

    7,10

    Other Resources

    PoEAA—Page 361

    Other Resources

    GoF—Page 331

    V

    alueObject

    Summary

    Handles objects whose equality is determined by the value of the objects' attributes, not by the identity of the objects.

    Reference Chapter

    2

    Other Resources

    PoEAA—Page 486 Evans DDD—Page 99

    View Helper Summary

    A class that helps the view by collecting data from the Model.

    Reference Chapter

    17

    Other Resources

    PoEAA—Page 355

    309

    310

    Pattern Quick Reference

    Book References • GoF - Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Design

    • Clifton Nock, Data Access Patterns, Addison-Wesley, 2004.

    Patterns: Elements of Reusable ObjectOriented Software. Addison-Wesley, 1995.

    • George Schlossnagle, Advanced PHP Programming, SAMS, 2004

    • PoEAA - Martin Fowler Patterns of Enterprise Application Architecture Addison-Wesley, 2003. • Harry Fuecks, PHP Anthology: Object

    • Alan Shalloway and James R. Trott, Design Patterns Explained, AddisonWesley, 2005

    Oriented PHP Solutions, SitePoint Pty. Lt., 2003

    • Rebecca Wirfs-Brock and Alan McKean, Roles, Responsibilities and

    • Allen Holub, Holub on Patterns: Learning Design Patterns by Looking at Code, Apress, 2004. • Robert Martin, Agile Software Development, Prentice Hall, 2003.

    Collaborations, Addison-Wesley, 2003.

    • Matt Zandstra, PHP 5 Objects, Patterns, Practice, Apress, 2004.

    B SimpleTest Testing Practices

    T

    HOUGH TESTING YOUR CODE is not specifically related to the implementation of design patterns, testing your code is such a powerful coding tool, it’s tightly integrated into the text of this book. The tests shown in this book ensure the code’s accuracy, but also implicitly describe how a section of code is supposed to work. The most practical way to approach automated testing is with a testing framework, most of which are derived from the design of JUnit (http://junit.org/). There are quite a number of PHP unit testing frameworks in existence, with 90 percent of them named PHPUnit (see http://www.google.com/search?q=phpunit). Sebastian Bergmann’s PHPUnit2 seems to be under the most active development, supporting PHP5 since July 2004 (http://pear.php.net/package/PHPUnit2/download). This book uses SimpleTest (http://sf.net/projects/simpletest/). It has excellent tutorials and documentation, and it supports web testing and the Mock Object testing pattern (covered in Chapter 6—The Mock Object Pattern). This appendix includes additional material that can help ramp up your testing skills. Specifically,

    314

    SimpleTest Testing Practices

    there’s a section about “scaffolding” and how to best structure and execute your test code; a section on the WebTestCase feature of SimpleTest, which allows you to test your entire web application as a “black box” just as an end-user would; and you can read about the Partial Mock Object technique, which can be used effectively to introduce Mock Objects into test code by systematically replacing internal Factory methods.

    Best Practices for Using SimpleTest SimpleTest is a unit testing framework developed by Marcus Baker. The current version of SimpleTest is coded for PHP4, but it runs on PHP5 with very minimal issues. You can look at the SimpleTest documentation and tutorials either on http://simpletest.org/ or http://www.lastcraft.com/simple_test.php. These provide a very thorough introduction to using the tool. The first step in building a test suite for a reasonable size project is to make a project-specific testing include file. The primary content of this file should be a subclass of UnitTestCase written specifically for your project. The first line of code should be a definition of the SIMPLE_TEST constant. SIMPLE_TEST is both a historical artifact and a useful feature: in past versions, SimpleTest used this constant itself, but this dependency has now been removed. Otherwise, for practical reasons, the constant can be used as an indication that you’re currently running a test (normally it’s not a good practice to alter your code’s behavior under testing conditions, but an example of where you might is to guard against exit() inside of your projects when running test cases) and can be used to record a library path.

    /** * relative path to SimpleTest *@ignore */ if (!defined(‘SIMPLE_TEST’)) define(‘SIMPLE_TEST’, ‘simpletest/’);

    Once this constant is defined, include the SimpleTest files you use in every test case:

    /**#@+ * SimpleTest includes */ require_once SIMPLE_TEST.’unit_tester.php’; require_once SIMPLE_TEST.’reporter.php’; require_once SIMPLE_TEST.’mock_objects.php’; require_once SIMPLE_TEST.’web_tester.php’; /**#@-*/

    SimpleTest Testing Practices

    Another best practice I have is to place all of my test cases in a subdirectory “tests/” immediately below my project root. (One problem with this practice is any code that relies on relative includes from this project root may break under test conditions. This can be solved by adding the parent directory “../” to the include path.) I also include any common setup for the application (such as base library includes, constant definitions, and so on).

    /** * modify php include path to include parent directory * * this is required because the tests are run from * the tests subdirectory and the application is run from * (and coded for) the parent directory * @ignore */ if (!defined(‘TEST_PATH_MODIFIED’)) { ini_set(‘include_path’, ‘..:’.ini_get(‘include_path’)); define(‘TEST_PATH_MODIFIED’, true); } /** *include standard setup file for this application */ require_once ‘setup.php’;

    Finally, I create a project-specific subclass of the UnitTestCase class to use for testing. You can then include assertions and utility functions unique to this project inside this class and have them available in all of your test cases.

    /** *UnitTestCase for myProject application *@packagemyProject *@subpackagetests */ class MyProjectUnitTestCase extends UnitTestCase { function projAssertSomething() {} function projHelperUtil() {} }

    I also highly recommend that you make your test cases as easy to run as possible. That means several things. First, you should be able to run a specific unit test case or a collection of cases for one aspect of your code, and you should be able to run every test that you’ve written. It is also helpful to be able to run your tests through the browser or via the command-line. Handling the latter is straightforward: detect if you are using the CLI interface and select the appro-

    315

    316

    SimpleTest Testing Practices

    priate test runner to execute the test:

    if (TextReporter::inCli()) { exit ($test->run(new TextReporter()) ? 0 : 1); } $test->run(new HtmlReporter());

    The ability to select and run individual tests or the entire test suite depends on how you organize your test case files and how you organize your tests. If you don’t include an index file and have auto indexing enabled in your web server, you can run an individual test case just by clicking on its filename. Here, each test case has to differentiate if it’s running singly or as part of a larger suite and behave appropriately. In addition, I find that I typically define more than one test case in each file, because I may define one UnitTestCase for the actual unit test and another as an integration test. Because of this possibility, I define even a single test case in standalone test files as group tests. Here is a trimmed down example:

    require_once ‘myprojunittestcase.php’; class TestSomething extends MyProjUnitTestCase { function TestSomething($name) { $this->UnitTestCase($name); } function setup() {} function teardown() {} function TestSomething() { $this->assertTrue(true, ‘this should pass’); } } class TestSomethingIntegration extends MyProjUnitTestCase { //... } //run if stand alone if (!isset($this)) { $test =& new GroupTest(‘Something Unit Test’); $test->addTestCase(new TestSomething ); $test->addTestCase(new TestSomethingIntegration); if (TextReporter::inCli()) { exit ($test->run(new TextReporter()) ? 0 : 1); } $test->run(new HtmlReporter()); }

    SimpleTest Testing Practices

    You also need a file to run the entire test suite for your project. I usually name this file the highly imaginative “run.php”. In this file, add each of the individual test case files to a single group test, using $this->addTestFile(...) for each test. A stub of this file looks like:

    define(‘SIMPLE_TEST’, ‘simpletest/’); require_once ‘myprojunittestcase.php’; $test =& new GroupTest(‘My Project Application Tests’); $test->addTestFile(‘testsomething.php’); //... set_time_limit(0); if (TextReporter::inCli()) { exit ($test->run(new TextReporter()) ? 0 : 1); } $test->run(new HtmlReporter());

    If your testing needs are more complicated than this, you might want to take a look at the scripts that run the Web Application Component Toolkit (WACT) test suite at http://wact.sf.net/test/ and http://cvs.sf.net/viewcvs.py/wact/wact/tests/.

    Mock Objects We covered Mock Objects in some depth in Chapter 6, but let’s review the concept again as part of this discussion on test practices. Mock Objects follow a five-step testing pattern: • Create the Mock Objects (generate the Mock Object code, instantiate). • Setup the state of the Mock Objects (return values, etc.). • Establish expectations for the Mock Objects (method X() is called with parameter ‘Y’, method Z() should never be called, and so on). • Invoke the code you are testing with the Mock Objects as parameters. • Verify the expectations of the Mock Objects. The classic example of how a Mock Object can be used is simulating database interactions inside of a class. The following example shows how to simulate the Oracle driver provided by the popular ADOdb (http://adodb.sf.net/) database access library (used as the database access layer for the database-related patterns presented in Chapters 14—16).

    317

    318

    SimpleTest Testing Practices

    // PHP5 code Function TestGetData() { $this->assertTrue(defined(‘SomeModel::DATA_FUNCT’) , ‘DATA_FUNCT constant defined’); $c = new MockADODBConnection($this); $rs = new MockADORecordSet($this); $test_parm = 5; $test_array = array( array(‘testkey’=>’testval1’) ,array(‘testkey’=>’testval2’) ); $rs->SetReturnValue(‘GetArray’, $test_array); $rs->ExpectOnce(‘GetArray’); $c->SetReturnReference(‘execute’, $rs); $expect = array(new WantedPatternExpectation( ‘/’.preg_quote(SomeModel::DATA_FUNCT,’/’).’/’) ,array(‘FOO’ => $test_parm)); $c->ExpectOnce(‘execute’, $expect); $o = new SomeModel($c); $this->assertEqual($o->getData($test_parm), $test_array); $rs->tally(); $c->tally(); }

    Notice that the example—like your code—must introduce the mock database connection object somewhere in the code. There are many ways to do this, and you have probably used one or more of them in the past: global variables, a Singleton class, or always passing the connection into the retrieval method. This code uses a hybrid approach, passing the connection to the object at the time of instantiation. (My personal approach has been to allow for an optional connection parameter, otherwise retrieving the object from a Factory). Lets walk through this test method step by step. The first assertion verifies a particular class constant exists. Both the class method being tested and the test case make use of this constant. The next two lines create $c and $rs, which are the Mock database connection and Mock result set, respectively. The test case itself is passed as an argument to Mock Objects when they are instantiated. The next two lines of code create variables to hold values used in the test. I don’t spend a lot of time getting creative here, just slap in some values that approximate the right type. (I did not model the number of records or the real field names of the records with reasonable values in the test data, I just put together an array or an associative array and used those simplified values for testing.) In most cases, you are just verifying that the expected value—meaning the one you put into the server

    SimpleTest Testing Practices

    stub—was returned by your tested code. (You can get into more specifics around the details of the real returned structure when you do integration testing.) Sometimes I add random values to the test data, particularly if I’ve implemented some kind of caching in the tested code and I want to verify that the cache is being cleared. The next line, $rs->SetReturnValue(‘GetArray’, $test_array) instructs the mock result set to return the $test_array any time it’s getArray() method is called, followed immediately by telling the mock to expect the getArray() method to be called exactly once in the tested code. The line $c->SetReturnReference(‘execute’, $rs) tells the mock database connection to return the mocked result set if anyone calls the execute() method. Where Mock Objects really kick into overdrive is in simulating failure. ( Just how gracefully does your application handle the database returning an error instead of the expected result set?) SimpleTest uses the static method Mock::generate() to create a class definition for a Mock Object. The generate() method takes the class name you want to mock and generates and evaluates the PHP code for the mock class. You then create Mock Objects inside of the test case by $mock =& new MockOriginalClass($this), where $this is the test case itself.

    Web Testing SimpleTest includes a WebTestCase class, similar in scope to jWebUnit (http://jwebunit.sourceforge.net/). WebTestCase provides a browser in a script, capable of fetching, validating, and manipulating the end content of your PHP application.



    WebTestCase The SimpleTest WebTestCase allows you to test the end result of your PHP application, with the web site with your PHP script acting as the user and browser. It is a PHP implementation of a web testing framework, like http://htmlunit.sourceforge.net/. The WebTestCase can fetch pages, follow links, validate the presence and default values of form elements, and submit forms, frames, HTTP response codes, and more.

    You can easily script actions like browsing to a main page, clicking on a “login” link, retrieving a form, and submitting a username and password. The features of WebTestCase are beneficial for both integration testing (of your final application), as well as for working with legacy code (providing you a test harness for major work at restructuring older scripts, ones that probably do not have unit tests of their own). Let’s build a WebTestCase for the “legacy” web application developed in Chapter 6—The Mock Object Pattern. The tests will serve as a safety harness as the sample code is refactored.

    319

    320

    SimpleTest Testing Practices

    Our Legacy Application The simple script below is typical and could appear in any number of PHP applications. The page generates a login for if the user has not yet logged in, acts as a form handler for the form, shows different content after a successful login, and provides a logout feature. Here’s the code to display a login form if the user is not yet logged in:




    Super secret member only content here. Logout

    Adding in the form handling capabilities, session startup and logout feature, and the whole script might look like:

    session_start(); define(‘SELF’, ‘http://’.$_SERVER[‘SERVER_NAME’].$_SERVER[‘PHP_SELF’]); if (array_key_exists(‘name’, $_REQUEST) && array_key_exists(‘passwd’, $_REQUEST) && ‘admin’ == $_REQUEST[‘name’]

    SimpleTest Testing Practices

    && ‘s3cr3t’ == $_REQUEST[‘passwd’]) { $_SESSION[‘name’] = ‘admin’; header(‘Location: ‘.SELF); } if (array_key_exists(‘clear’, $_REQUEST)) { unset($_SESSION[‘name’]); } if (array_key_exists(‘name’, $_SESSION) && $_SESSION[‘name’]) { ?>

    Welcome
    Super secret member only content here. Logout