www.wrox.com $44.99 USA $53.99 CAN Recommended Computer Book Categories Programming Languages PHP ISBN: 978-0-470-49670-1 Wrox Professional guides are planned and written by working programmers to meet the real-world needs of programmers, developers, and IT professionals. Focused and relevant, they address the issues technology professionals face every day. They provide examples, practical solutions, and expert education in new technologies, all designed to help programmers do a better job. PHP is often considered to be the cornerstone for building flexible and feature-rich web applications. Because of its relatively simple initial learning curve, newer programmers can easily create applications in PHP from the ground up. As these applications scale, however, the maintenance can seem daunting. Luckily, the mature programming community has something to offer: Design Patterns. These repeatable solutions for common problems have been applied for years to other programming languages. This book aims to bridge the gap between the PHP and the older programming language by applying those tried and tested Design Patterns to native PHP applications. This book starts with an introduction to Design Patterns, describes their use and importance, and details where you've seen them already. It continues through a host of Design Patterns with code examples and explanations. Finally, an in-depth case study shows you how to plan your next application using Design Patterns, how to program those patterns in PHP, and how to revise and refactor an existing block of code using Design Patterns from the book. The author approaches the concepts in technical yet usable formats that are familiar to both programmers who are self-taught and those with more extensive formal education. What you will learn from this book ● What Design Patterns are and why they matter in PHP ● Commonly established PHP Library usage of Design Patterns ● Faster and more efficient object creation with Design Patterns like Builder, Façade, Prototype and Template ● Decoupled architecture using Data Access Object and Proxy Design Patterns ● Better code flow and control using the Delegate, Factory and Singleton Design Patterns ● How to implement Visitor and Decorator Design Patterns to enhance objects without modifying their core ● Comprehensive plug-in creation techniques using Observer Design Pattern ● How to plan application architecture using Design Patterns ● How to program a case study web site from start to finish with Design Pattern based coding ● How to recognize architectural benefits from Design Patterns in an existing application ● Methods to further strengthen code from case study analysis Who this book is for This book is for experienced PHP programmers who are looking to enhance the quality of their code architecture with Design Patterns. Professional PHP Design Patterns Saray spine=.5625" PHP Design Patterns Professional Updates, source code, and Wrox technical support at www.wrox.com PHP Design Patterns Aaron Saray Professional Wrox Programmer to Programmer TM Wrox Programmer to Programmer TM ffirs.indd ivffirs.indd iv 7/16/09 9:11:09 AM7/16/09 9:11:09 AM Do wnl oa d fr om Wo w! e Boo k <w ww .wo we bo ok .co m> Professional PHP Design Patterns Introduction xxi Part I: Getting Acquainted with Design Patterns and PHP Chapter 1: Understanding Design Patterns 3 Chapter 2: Using Tools Already In Your Arsenal 11 Part II: Reference Material Chapter 3: Adapter Pattern 25 Chapter 4: Builder Pattern 31 Chapter 5: Data Access Object Pattern 37 Chapter 6: Decorator Pattern 43 Chapter 7: Delegate Pattern 49 Chapter 8: Façade Pattern 55 Chapter 9: Factory Pattern 61 Chapter 10: Interpreter Pattern 67 Chapter 11: Iterator Pattern 73 Chapter 12: Mediator Pattern 79 Chapter 13: Observer Pattern 85 Chapter 14: Prototype Pattern 91 Chapter 15: Proxy Pattern 97 Chapter 16: Singleton Pattern 103 Chapter 17: Strategy Pattern 109 Chapter 18: Template Pattern 115 Chapter 19: Visitor Pattern 121 Continues ffirs.indd iffirs.indd i 7/16/09 9:11:08 AM7/16/09 9:11:08 AM Part III: PHP Design Case Study Chapter 20: Requirements Analysis 129 Chapter 21: Choosing Design Patterns and Planning 141 Chapter 22: Programming the Application 159 Chapter 23: Improving with More Design Patterns 227 Index 241 ffirs.indd iiffirs.indd ii 7/16/09 9:11:09 AM7/16/09 9:11:09 AM Professional PHP Design Patterns ffirs.indd iiiffirs.indd iii 7/16/09 9:11:09 AM7/16/09 9:11:09 AM ffirs.indd ivffirs.indd iv 7/16/09 9:11:09 AM7/16/09 9:11:09 AM Professional PHP Design Patterns Aaron Saray Wiley Publishing, Inc. ffirs.indd vffirs.indd v 7/16/09 9:11:09 AM7/16/09 9:11:09 AM Professional PHP Design Patterns Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-49670-1 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permis- sion of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley .com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warran- ties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Library of Congress Control Number: 2009931463 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. ffirs.indd viffirs.indd vi 7/16/09 9:11:09 AM7/16/09 9:11:09 AM To the big one for always being a little bit better, and the little one for reminding me that I can be. ffirs.indd viiffirs.indd vii 7/16/09 9:11:10 AM7/16/09 9:11:10 AM Do wnl oa d fr om Wo w! e Boo k <w ww .wo we bo ok .co m> ffirs.indd viiiffirs.indd viii 7/16/09 9:11:10 AM7/16/09 9:11:10 AM [...]... Acquainted with Design Patterns and PHP Chapter 1: Understanding Design Patterns What Are They? An All Too Common Example Design Patterns Are Solving the Same Problem Design Patterns Are Around You All the Time The Common Parts of a Design Pattern What Design Patterns Are Not Design Patterns Are Not Plug and Play Design Patterns are Maintainable But Not Always Most Efficient Design Patterns are a Vehicle,... Refactoring End Design Pattern Demonstration Why Use Design Patterns in PHP? Summary Chapter 2: Using Tools Already In Your Arsenal Patterns in Existing Frameworks Design Patterns in PEAR Design Patterns in the Zend Framework Design Patterns in Doctrine The Standard PHP Library 3 3 3 4 5 6 7 7 8 8 8 9 9 11 11 12 13 15 15 SPL Observer and SPL Subject SPL Iterators 15 15 Using Eclipse PDT with Patterns 17... 23: Improving with More Design Patterns Working with Contacts Import Outlook Contact Adapter Building the Contacts Array Removing Logic from Views Modifying the Single View of a Contact Try Implementing Design Patterns Design Patterns and Error Checking Design Patterns and Contact Administration Design Patterns and View Types Design Patterns and Deleting Objects Share Your Design Pattern Work 141 141... 7/16/09 9:12:08 AM Professional PHP Design Patterns flast.indd xxvii 7/16/09 9:12:08 AM flast.indd xxviii 7/16/09 9:12:08 AM Part I Download from Wow! eBook Getting Acquainted with Design Patterns and PHP Chapter 1: Understanding Design Patterns Chapter 2: Using Tools Already In Your Arsenal c01.indd 1 7/17/09 12:17:41 PM c01.indd 2 7/17/09 12:17:43 PM Understanding Design Patterns Usually... introduction to Design Patterns and a call to be dedicated to using them in PHP Those talented PHP programmers out in the world are always hungry for new knowledge This chapter aims to expand their realm of hunger from just PHP- based concepts to the more architecturally sound Design Patterns realm The second chapter focuses more on the tools that are available in PHP to build the roots of these Design Pattern... This is exactly what Design Patterns are: reusable solutions for these common problems No book mentioning Design Patterns would be complete without the reference to the Gang of Four: Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, authors of the original Design Patterns book After a considerable amount of time in the field, they started noticing particular patterns of design emerging from... duplication Finally, after the software analysis, the team started discovering patterns in their seemingly jumbled code base This is the beginning of this team’s foray into Design Patterns Design Patterns Are Solving the Same Problem In the previous example, Steve’s team stumbled into the first important part of the Design Pattern concept Patterns are not intentionally created in software development They are... Pattern concepts Reviewing such things as the intermediate and advanced OOP features of PHP, the Standard PHP Library, and the existing open source PHP frameworks that are common will help make concrete the coupling of PHP and Design Patterns Reference Chapters The reference chapters are the middle chapters or the actual Design Pattern meat-and-potato portion of the book They will be broken down into four... already been using patterns 5 c01.indd 5 7/17/09 12:17:44 PM Part I: Getting Acquainted with Design Patterns and PHP Even more detailed and closer to the root patterns are examples found in your favorite PEAR or other framework libraries For example, using PEAR DB is an example of putting a Design Pattern into use (notably the factory method) The Zend Framework also uses various different patterns such... easily decided.) In order for the book to reach the widest audience for implementing Design Patterns in PHP, while not becoming a PHP language reference, the following guidelines apply to the audience requirements of this book The reader: ❑ Must be thoroughly experienced in the PHP language or at least have http:/ /php. net bookmarked Some examples may use functions that a beginning programmer may not . with Design Patterns. Professional PHP Design Patterns Saray spine=.5625" PHP Design Patterns Professional Updates, source code, and Wrox technical support at www.wrox.com PHP Design Patterns Aaron. ww .wo we bo ok .co m> Professional PHP Design Patterns Introduction xxi Part I: Getting Acquainted with Design Patterns and PHP Chapter 1: Understanding Design Patterns 3 Chapter 2: Using. Acquainted with Design Patterns and PHP Chapter 1: Understanding Design Patterns 3 What Are They? 3 An All Too Common Example 3 Design Patterns Are Solving the Same Problem 4 Design Patterns Are