Table Of Contentcyan yelloW
MaGenTa Black
panTone 123 c
Books for professionals By professionals® The eXperT’s Voice® in open source
companion
Author of eBook Available
PHP Objects, Patterns, and Practice
SAMS Teach Yourself PHP SecOnd editiOn
in 24 Hours P
Dear Reader,
In recent years, PHP has embraced the object-oriented revolution, both in
terms of usage patterns and language features. Still, though, most books about H
object-oriented design focus on languages like Java and C++. But one of the nice
things about designing with objects is how concepts can travel among languages. In PHP Objects,
this book, I explore object-oriented programming and design from the standpoint
of a PHP programmer. P
I begin with an overview of PHP’s object-oriented features, introducing key
topics like class declaration, object instantiation, inheritance, and method
and property encapsulation. You’ll also learn about advanced topics including
static methods and properties, abstract classes, interfaces, exception handling, a O Patterns,
object cloning, and more. You’ll benefit from an extensive discussion of best n and
b
practices in object-oriented design too. d
The next part of this book is devoted to design patterns, a subject that naturally j
Pe
complements object orientation. I introduce pattern concepts and show you
how to implement several key patterns in your PHP applications, and I include rc
chapters on enterprise and database patterns. at Practice
In the last segment of this book, I introduce a number of great utilities that cs
help you document, manage, test, and build your PHP applications, including ,
t
Phing, PHPUnit2, PHPDocumentor, PEAR, and CVS. iP
I wrote PHP Objects, Patterns, and Practice to give you the kind of help I c
a
would have liked when I first discovered object-oriented programming and e
design patterns, since I learned a lot of lessons the hard way, in both code t
t
design and good project practice.
e
Matt Zandstra r
n Build powerful code by mastering PHP’s
s object-oriented enhancements, design
,
THE APRESS ROADMAP patterns, and essential development tools
companion eBook
Beginning PHP and PHP 5 Recipes: A
MySQL 5, Second Edition Problem-Solution Approach
PHP Objects, Patterns, and
Beginning Joomla! Practice, Second Edition SecOnd SecOnd editiOn
See last page for details editiOn
on $10 eBook version Beginning PHP 5
Pro PHP
and MySQL E-Commerce
Z
Matt Zandstra
SOURCE CODE ONLINE ISBN-13: 978-1-59059-909-9 an
www.apress.com ISBN-10: 1-59059-909-8 d
54499 s
t
r
a
US $44.99
Shelve in
PHP
User level:
9 781590 599099
Intermediate–Advanced
this print for content only—size & color not accurate spine = 0.9682" 512 page count
9098_FM.qxd 11/17/07 11:27 AM Page i
PHP Objects, Patterns,
and Practice
Second Edition
Matt Zandstra
9098_FM.qxd 11/17/07 11:27 AM Page ii
PHP Objects,Patterns,and Practice,Second Edition
Copyright © 2008 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-59059-909-9
ISBN-10 (pbk): 1-59059-909-8
ISBN-13 (electronic): 978-1-4302-0466-4
ISBN-10 (electronic): 1-4302-0466-4
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 atrademark symbol with every occurrence
of atrademarked 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.
Lead Editor: Tom Welsh
Technical Reviewer: Tolan Blundell
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Richard Dal Porto
Copy Editors: Heather Lang, Benjamin Berg
Associate Production Director: Kari Brooks-Copony
Senior Production Editor: Laura Cheu
Compositor: Kinetic Publishing Services, LLC
Proofreader: Nancy Riddiough
Indexer: Becky Hornyak
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
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 [email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley,
CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.
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 http://www.apress.com.
9098_FM.qxd 11/17/07 11:27 AM Page iii
For Louise,who is the whole point
9098_FM.qxd 11/17/07 11:27 AM Page iv
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction to the First Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
PART 1 ■ ■ ■ Introduction
■ CHAPTER 1 PHP:Design and Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
PART 2 ■ ■ ■ Objects
■ CHAPTER 2 PHP and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
■ CHAPTER 3 Object Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
■ CHAPTER 4 Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
■ CHAPTER 5 Object Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
■ CHAPTER 6 Objects and Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
PART 3 ■ ■ ■ Patterns
■ CHAPTER 7 What Are Design Patterns? Why Use Them? . . . . . . . . . . . . . . . . . . . 121
■ CHAPTER 8 Some Pattern Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
■ CHAPTER 9 Generating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■ CHAPTER 10 Patterns for Flexible Object Programming . . . . . . . . . . . . . . . . . . . . . 167
■ CHAPTER 11 Performing and Representing Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . 187
■ CHAPTER 12 Enterprise Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
■ CHAPTER 13 Database Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
iv
9098_FM.qxd 11/17/07 11:27 AM Page v
PART 4 ■ ■ ■ Practice
■ CHAPTER 14 Good (and Bad) Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
■ CHAPTER 15 An Introduction to PEAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
■ CHAPTER 16 Generating Documentation with phpDocumentor . . . . . . . . . . . . . . 353
■ CHAPTER 17 Version Control with CVS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
■ CHAPTER 18 Testing with PHPUnit. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
■ CHAPTER 19 Automated Build with Phing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
PART 5 ■ ■ ■ Conclusion
■ CHAPTER 20 Objects,Patterns,Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
PART 6 ■ ■ ■ Appendixes
■ APPENDIX A Bibliography. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
■ APPENDIX B A Simple Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
v
9098_FM.qxd 11/17/07 11:27 AM Page vi
9098_FM.qxd 11/17/07 11:27 AM Page vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xviii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction to the First Edition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
PART 1 ■ ■ ■ Introduction
■ CHAPTER 1 PHP: Design and Management. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
PHP and Other Languages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
About This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Patterns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What’s New in the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
PART 2 ■ ■ ■ Objects
■ CHAPTER 2 PHP and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
The Accidental Success of PHP Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
In the Beginning:PHP/FI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Syntactic Sugar:PHP 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
PHP 4 and the Quiet Revolution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Change Embraced:PHP 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Into the Future:PHP 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Advocacy and Agnosticism:The Object Debate. . . . . . . . . . . . . . . . . . . . . . 15
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
vii
9098_FM.qxd 11/17/07 11:27 AM Page viii
viii ■ CONTENTS
■ CHAPTER 3 Object Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Classes and Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
A First Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
A First Object (or Two). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Setting Properties in a Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Working with Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Creating a Constructor Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Arguments and Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Primitive Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Taking the Hint:Object Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
The Inheritance Problem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Working with Inheritance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Public,Private,and Protected:Managing Access to
Your Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
■ CHAPTER 4 Advanced Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Static Methods and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Constant Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Exceptions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Final Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Working with Interceptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Defining Destructor Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Copying Objects with __clone() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Defining String Values for Your Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
■ CHAPTER 5 Object Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
PHP and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
PHP and Namespaces. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Include Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Autoload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Description:k Available. Build powerful code by mastering PHP's PHP Objects, Patterns, and Practice, Second Edition .. A Design Pattern Defines a Problem.