PHP Objects, Patterns and Practice, 3rd Edition

  ®

  THE EXPERT’S VOICE

  PHP Objects, and

  Patterns, Practice Build powerful code by mastering PHP’s object-oriented enhancements, design patterns, and essential development tools

  THIRD EDITION SOURCE CODE ONLINE Matt Zandstra www.apress.com US $44.99

   PHP Objects, Patterns, and Practice Third Edition

  ■ ■ ■ Matt Zandstra

  PHP Objects, Patterns, and Practice, Third Edition

  Copyright © 2010 by Matt Zandstra All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

  ISBN-13 (pbk): 978-1-4302-2925-4

  ISBN-13 (electronic): 978-1-4302-2926-1 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

  President and Publisher: Paul Manning Lead Editor: Michelle Lowman, Matt Wade Technical Reviewer: Wes Hunt Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell,

  Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh

  Coordinating Editor: Jim Markham Copy Editor: Tracy Brown Collins Compositor: MacPS, LLC Indexer: Toma Mulligan Artist: April Milne Cover Designer: Anna Ischenko

  Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders

   Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

  The source code for this book is available to readers at questions pertaining to this book in order to successfully download the code. ii

Contents at a Glance

  ■

  Chapter 12: Enterprise Patterns ..................................................................................................

  ■

  Chapter 13: Database Patterns....................................................................................................

  ■

  Chapter 14: Good (and Bad) Practice ..........................................................................................

  ■

  Chapter 15: An Introduction to PEAR and Pyrus..........................................................................

  ■

  Chapter 16: Generating Documentation with phpDocumentor ....................................................

  ■

  Chapter 17: Version Control with Subversion..............................................................................

  Chapter 18: Testing with PHPUnit................................................................................................

  Chapter 11: Performing and Representing Tasks........................................................................

  ■

  Chapter 19: Automated Build with Phing ....................................................................................

  ■

  Chapter 20: Continuous Integration.............................................................................................

  ■

  Chapter 21: Objects, Patterns, Practice ......................................................................................

  ■

  Appendix A: Bibliography ............................................................................................................

  ■

  Appendix B: A Simple Parser.......................................................................................................

  ■

  Index............................................................................................................................................

  ■

  ■

  Contents at a Glance.......................................................................................................................

  Chapter 3: Object Basics ...............................................................................................................

  ■ Contents .......................................................................................................................................... ■

  About the Technical Reviewer ....................................................................................................

  ■

  Acknowledgments ........................................................................................................................

  ■

  Introduction to the Third Edition ...................................................................................................

  Part 1: Introduction.............................................................................................................................

  ■

  Chapter 1: PHP: Design and Management .......................................................................................

  ■

  Chapter 2: PHP and Objects...........................................................................................................

  ■

  ■

  Chapter 10: Patterns for Flexible Object Programming...............................................................

  Chapter 4: Advanced Features.......................................................................................................

  ■

  Chapter 5: Object Tools..................................................................................................................

  ■

  Chapter 6: Objects and Design ......................................................................................................

  ■

  Chapter 7: What Are Design Patterns? Why Use Them? ..............................................................

  ■

  Chapter 8: Some Pattern Principles ............................................................................................

  ■

  Chapter 9: Generating Objects.....................................................................................................

  ■

  ■

  

Contents

  ■ Contents at a Glance ............................................................................................ ■ About the Author ............................................................................................... ■ Acknowledgments..............................................................................................

  Introduction to the Third Edition ......................................................................... ■

  Part 1: Introduction .................................................................................................. ■

  The Problem.................................................................................................................... About This Book ..............................................................................................................

  Objects ................................................................................................................................................... Patterns.................................................................................................................................................. Practice .................................................................................................................................................. What’s New in the Third Edition.............................................................................................................

  Summary.........................................................................................................................

  Part 2: Objects.......................................................................................................... ■

  The Accidental Success of PHP Objects .......................................................................

  In the Beginning: PHP/FI ...................................................................................................................... Syntactic Sugar: PHP 3 ........................................................................................................................ PHP 4 and the Quiet Revolution ...........................................................................................................

  Change Embraced: PHP 5 ....................................................................................................................

  

Into the Future ..............................................................................................................

Summary.......................................................................................................................

  

Chapter 3: Object Basics .....................................................................................

  

Classes and Objects......................................................................................................

  A First Class ......................................................................................................................................... A First Object (or Two) .........................................................................................................................

  

Setting Properties in a Class........................................................................................

Working with Methods ..................................................................................................

  Creating a Constructor Method ............................................................................................................

  

Arguments and Types ...................................................................................................

  Primitive Types .................................................................................................................................... Taking the Hint: Object Types ..............................................................................................................

  

Inheritance ....................................................................................................................

  The Inheritance Problem ...................................................................................................................... Working with Inheritance ..................................................................................................................... Public, Private, and Protected: Managing Access to Your Classes ......................................................

  

Summary.......................................................................................................................

  

Chapter 4: Advanced Features ............................................................................

  

Static Methods and Properties......................................................................................

Abstract Classes ...........................................................................................................

Late Static Bindings: The static Keyword..................................................................

Handling Errors .............................................................................................................

  Exceptions............................................................................................................................................

  

Final Classes and Methods ...........................................................................................

Defining Destructor Methods ........................................................................................

  Copying Objects with __clone() .................................................................................... Callbacks, Anonymous Functions and Closures ...........................................................

  Chapter 5: Object Tools ....................................................................................... ■

  PHP and Packages ........................................................................................................

  PHP Packages and Namespaces.......................................................................................................... Autoload ...............................................................................................................................................

The Class and Object Functions.................................................................................... 81

  Looking for Classes.............................................................................................................................. Learning About an Object or Class....................................................................................................... Learning About Methods ...................................................................................................................... Learning About Properties ................................................................................................................... Learning About Inheritance.................................................................................................................. Method Invocation................................................................................................................................

The Reflection API......................................................................................................... 87

  Getting Started ..................................................................................................................................... Time to Roll Up Your Sleeves ............................................................................................................... Examining a Class .............................................................................................................................. Examining Methods.............................................................................................................................. Examining Method Arguments............................................................................................................. Using the Reflection API.......................................................................................................................

  Summary.......................................................................................................................

  Chapter 6: Objects and Design ............................................................................ ■

  Defining Code Design....................................................................................................

  Responsibility..................................................................................................................................... Cohesion ............................................................................................................................................ Coupling ............................................................................................................................................. Orthogonality......................................................................................................................................

  Choosing Your Classes................................................................................................

  

Polymorphism .............................................................................................................

Forget How to Do It .....................................................................................................

  Code Duplication ................................................................................................................................ The Class Who Knew Too Much ......................................................................................................... The Jack of All Trades........................................................................................................................ Conditional Statements......................................................................................................................

  

The UML ......................................................................................................................

  Class Diagrams .................................................................................................................................. Sequence Diagrams ...........................................................................................................................

  

Summary.....................................................................................................................

  Part 3: Patterns .................................................................................................... ■

  

What Are Design Patterns? .........................................................................................

  Name.................................................................................................................................................. The Problem....................................................................................................................................... The Solution ....................................................................................................................................... Consequences....................................................................................................................................

The Gang of Four Format ............................................................................................ 126 Why Use Design Patterns?.......................................................................................... 127

  A Design Pattern Defines a Problem .................................................................................................. A Design Pattern Defines a Solution .................................................................................................. Design Patterns Are Language Independent...................................................................................... Patterns Define a Vocabulary............................................................................................................. Patterns Are Tried and Tested ........................................................................................................... Patterns Are Designed for Collaboration ............................................................................................ Design Patterns Promote Good Design ..............................................................................................

  

PHP and Design Patterns ............................................................................................

  Chapter 8: Some Pattern Principles .................................................................. ■

  The Pattern Revelation................................................................................................

  The Problem....................................................................................................................................... Using Composition .............................................................................................................................

  Decoupling ..................................................................................................................

  The Problem....................................................................................................................................... Loosening Your Coupling ...................................................................................................................

  Code to an Interface, Not to an Implementation .........................................................

Patternitis.................................................................................................................... 143 The Patterns................................................................................................................ 143

  Patterns for Generating Objects......................................................................................................... Patterns for Organizing Objects and Classes ..................................................................................... Task-Oriented Patterns ...................................................................................................................... Enterprise Patterns ............................................................................................................................ Database Patterns..............................................................................................................................

  Summary.....................................................................................................................

  Chapter 9: Generating Objects .......................................................................... ■

  Problems and Solutions in Generating Objects...........................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  Factory Method Pattern ..............................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

Abstract Factory Pattern ............................................................................................. 157

  The Problem....................................................................................................................................... Implementation ..................................................................................................................................

  Consequences.................................................................................................................................... Prototype............................................................................................................................................ The Problem....................................................................................................................................... Implementation ..................................................................................................................................

  

But That’s Cheating! ...................................................................................................

Chapter 10: Patterns for Flexible Object Programming ....................................

  ■

Structuring Classes to Allow Flexible Objects.............................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences.................................................................................................................................... Composite in Summary......................................................................................................................

  

The Decorator Pattern.................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

The Facade Pattern .....................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

Summary.....................................................................................................................

  

Chapter 11: Performing and Representing Tasks .............................................

  

The Interpreter Pattern................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Interpreter Issues...............................................................................................................................

  

The Strategy Pattern ...................................................................................................

  The Problem....................................................................................................................................... Implementation ..................................................................................................................................

  

The Observer Pattern ..................................................................................................

  Implementation ..................................................................................................................................

  The Visitor Pattern ......................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Visitor Issues......................................................................................................................................

  The Command Pattern ................................................................................................

  The Problem....................................................................................................................................... Implementation ..................................................................................................................................

  Summary.....................................................................................................................

  Chapter 12: Enterprise Patterns........................................................................ ■

  Architecture Overview.................................................................................................

  The Patterns....................................................................................................................................... Applications and Layers .....................................................................................................................

  Cheating Before We Start............................................................................................

  Registry .............................................................................................................................................. Implementation ..................................................................................................................................

  The Presentation Layer ...............................................................................................

  Front Controller .................................................................................................................................. Application Controller......................................................................................................................... Page Controller .................................................................................................................................. Template View and View Helper ........................................................................................................

  The Business Logic Layer ...........................................................................................

  Transaction Script.............................................................................................................................. Domain Model ....................................................................................................................................

  Summary.....................................................................................................................

  Chapter 13: Database Patterns ......................................................................... ■

The Data Layer ............................................................................................................ 275 Data Mapper ............................................................................................................... 275

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

Identity Map ................................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

Unit of Work ................................................................................................................ 291

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences.................................................................................................................................... Lazy Load ........................................................................................................................................... The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

Domain Object Factory................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

The Identity Object ......................................................................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

The Selection Factory and Update Factory Patterns ...................................................

  The Problem....................................................................................................................................... Implementation .................................................................................................................................. Consequences....................................................................................................................................

  

What’s Left of Data Mapper Now? ..............................................................................

  Part 4: Practice .................................................................................................... ■

  

Beyond Code ...............................................................................................................

Borrowing a Wheel.....................................................................................................

Playing Nice ................................................................................................................

  Giving Your Code Wings .............................................................................................. Testing ........................................................................................................................ Summary.....................................................................................................................

  Chapter 15: An Introduction to PEAR and Pyrus ............................................... ■

  What Is PEAR?............................................................................................................. Installing a Package...................................................................................................

  PEAR Channels...................................................................................................................................

  Using a PEAR Package................................................................................................

  Handling PEAR Errors.........................................................................................................................

  Creating Your Own PEAR Package ..............................................................................

  package.xml....................................................................................................................................... Package Elements.............................................................................................................................. The contents Element ..................................................................................................................... Dependencies .................................................................................................................................... Tweaking Installation with phprelease ........................................................................................... Preparing a Package for Shipment .................................................................................................... Setting Up Your Own Channel ............................................................................................................

  Summary.....................................................................................................................

  Chapter 16: Generating Documentation with phpDocumentor.......................... ■

  Why Document?.......................................................................................................... Generating Documentation ......................................................................................... Documenting Classes ................................................................................................. Documenting Properties .............................................................................................

  

Creating Links in Documentation................................................................................

  

Chapter 17: Version Control with Subversion ...................................................

  

Chapter 18: Testing with PHPUnit .....................................................................

  Writing Web Tests .......................................................................................................

  Refactoring a Web Application for Testing......................................................................................... Simple Web Testing ........................................................................................................................... Introducing Selenium .........................................................................................................................

  A Note of Caution ........................................................................................................ Chapter 19: Automated Build with Phing ..........................................................

  ■ What Is Phing? ............................................................................................................ Composing the Build Document..................................................................................

  Targets ............................................................................................................................................... Properties........................................................................................................................................... Types.................................................................................................................................................. Tasks..................................................................................................................................................

  Summary.....................................................................................................................

  Chapter 20: Continuous Integration .................................................................. ■

  What Is Continuous Integration? .................................................................................

  Preparing a Project for CI ...................................................................................................................

  CruiseControl and phpUnderControl............................................................................

  Installing CruiseControl ...................................................................................................................... Installing phpUnderControl................................................................................................................. Installing Your Project ........................................................................................................................

  Summary.....................................................................................................................

  Part 5: Conclusion ................................................................................................ ■

Objects ........................................................................................................................ 453

  Choice ................................................................................................................................................ Encapsulation and Delegation............................................................................................................ Decoupling ......................................................................................................................................... Reusability ......................................................................................................................................... Aesthetics ..........................................................................................................................................

  

Patterns.......................................................................................................................

  What Patterns Buy Us ........................................................................................................................ Patterns and Principles of Design ......................................................................................................

Practice....................................................................................................................... 458

  Testing ............................................................................................................................................... Documentation................................................................................................................................... Version Control................................................................................................................................... Automated Build................................................................................................................................. Continuous Integration....................................................................................................................... What I Missed ....................................................................................................................................

  

Summary.....................................................................................................................

  

Appendix A: Bibliography..................................................................................

Articles........................................................................................................................

  ■

Appendix B: A Simple Parser ............................................................................