Pro PHPPatterns, Frameworks, Testing and More
■ ■ ■
Kevin MCArthur
McArthur_819-9FRONT.fm Page i Thursday, March 6, 2008 9:28 AM
Pro PHP: Patterns, Frameworks, Testing and More
Copyright © 2008 by Kevin McArthur
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-819-1
ISBN-10 (pbk): 1-59059-819-9
ISBN-13 (electronic): 978-1-4302-0279-0
ISBN-10 (electronic): 1-4302-0279-3
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.
Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc.
Lead Editors: Jason Gilmore, Tom WelshTechnical Reviewer: Jeffrey SambellsEditorial Board: Clay Andres, Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell,
Jonathan Gennick, Kevin Goff, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Beth ChristmasCopy Editor: Marilyn SmithAssociate Production Director: Kari Brooks-CoponyProduction Editor: Katie StenceCompositor: Susan GlinertProofreader: Lisa HamiltonIndexer: Broccoli Information ManagementArtist: April MilneCover Designer: Kurt KramesManufacturing 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.
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 http://www.apress.com/info/bulksales.
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.
McArthur_819-9FRONT.fm Page ii Thursday, March 6, 2008 9:28 AM
Jill—my wife, my muse, and my raison d’être—I write for you.
McArthur_819-9FRONT.fm Page iii Thursday, March 6, 2008 9:28 AM
iv
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
PART 1 ■ ■ ■ OOP and Patterns ■CHAPTER 1 Abstract Classes, Interfaces, and Programming by Contract . . . . . 3
■CHAPTER 2 Static Variables, Members, and Methods . . . . . . . . . . . . . . . . . . . . . . 11
■CHAPTER 3 Singleton and Factory Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
■CHAPTER 4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
■CHAPTER 5 What's New in PHP 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
PART 2 ■ ■ ■ Testing and Documentation■CHAPTER 6 Documentation and Coding Conventions . . . . . . . . . . . . . . . . . . . . . . 55
■CHAPTER 7 Reflection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
■CHAPTER 8 Testing, Deployment, and Continuous Integration . . . . . . . . . . . . . 105
PART 3 ■ ■ ■ The Standard PHP Library (SPL)■CHAPTER 9 Introduction to SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
■CHAPTER 10 SPL Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
■CHAPTER 11 SPL File and Directory Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■CHAPTER 12 SPL Array Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 13 SPL Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
McArthur_819-9FRONT.fm Page iv Thursday, March 6, 2008 9:28 AM
v
PART 4 ■ ■ ■ The Model-View-Controller (MVC) Pattern
■CHAPTER 14 MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
■CHAPTER 15 Introduction to the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . 215
■CHAPTER 16 Advanced Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
■CHAPTER 17 The Zend Framework Applied . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
PART 5 ■ ■ ■ Web 2.0 ■CHAPTER 18 Ajax and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
■CHAPTER 19 Introduction to Web Services with SOAP . . . . . . . . . . . . . . . . . . . . . 285
■CHAPTER 20 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
■CHAPTER 21 Certificate Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
McArthur_819-9FRONT.fm Page v Thursday, March 6, 2008 9:28 AM
McArthur_819-9FRONT.fm Page vi Thursday, March 6, 2008 9:28 AM
vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
PART 1 ■ ■ ■ OOP and Patterns ■CHAPTER 1 Abstract Classes, Interfaces, and Programming
by Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
The instanceof Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Programming by Contract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
■CHAPTER 2 Static Variables, Members, and Methods . . . . . . . . . . . . . . . . 11
Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Static Usage in Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Static Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Paamayim Nekudotayim . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Static Debate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
■CHAPTER 3 Singleton and Factory Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Responsibility and the Singleton Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 21
The Factory Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
The Image Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The Portable Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
McArthur_819-9FRONT.fm Page vii Thursday, March 6, 2008 9:28 AM
viii ■CO N T E N T S
■CHAPTER 4 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Implementing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Exception Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Extending Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Logging Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Logging Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Defining an Uncaught Exception Handler. . . . . . . . . . . . . . . . . . . . . . 36
Exception Overhead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Error Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Type Hinting and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Rethrowing Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
■CHAPTER 5 What's New in PHP 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
PHP Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Unicode in PHP 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Unicode Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Unicode Collations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Late Static Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Dynamic Static Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Ternary Assignment Shorthand (ifsetor) . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
XMLWriter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
PART 2 ■ ■ ■ Testing and Documentation■CHAPTER 6 Documentation and Coding Conventions . . . . . . . . . . . . . . . . 55
Coding Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
PHP Comments and Lexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Types of Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
More About Doccomments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Lexing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
PHPDoc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
McArthur_819-9FRONT.fm Page viii Thursday, March 6, 2008 9:28 AM
■C ON TE N TS ix
DocBook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Creating an XML File for DocBook . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Parsing a DocBook File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Using DocBook Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
■CHAPTER 7 Reflection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Introducing the Reflection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Retrieving User-Declared Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Understanding the Reflection Plug-in Architecture . . . . . . . . . . . . . . 76
Parsing Reflection-Based Documentation Data . . . . . . . . . . . . . . . . . . . . 81
Installing the Docblock Tokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Accessing Doccomment Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Tokenizing Doccomment Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Parsing the Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Extending the Reflection API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Integrating the Parser with the Reflection API . . . . . . . . . . . . . . . . . . 86
Extending Reflection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Updating the Parser to Handle In-Line Tags . . . . . . . . . . . . . . . . . . . 96
Adding Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
■CHAPTER 8 Testing, Deployment, and Continuous Integration . . . . . . 105
Subversion for Version Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Installing Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Setting Up Subversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Committing Changes and Resolving Conflicts . . . . . . . . . . . . . . . . . 108
Enabling Subversion Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
PHPUnit for Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Installing PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Creating Your First Unit Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Understanding PHPUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Phing for Deployment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Installing Phing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Writing a Phing Deployment Script . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Xinc, the Continuous Integration Server . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Installing Xinc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Creating the Xinc Configuration File . . . . . . . . . . . . . . . . . . . . . . . . . 119
Starting Xinc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
McArthur_819-9FRONT.fm Page ix Thursday, March 6, 2008 9:28 AM
x ■CO N T E N T S
Xdebug for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Installing Xdebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Tracing with Xdebug. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Profiling with Xdebug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Checking Code Coverage with Xdebug . . . . . . . . . . . . . . . . . . . . . . 123
Remote Debugging with Xdebug. . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
PART 3 ■ ■ ■ The Standard PHP Library (SPL)■CHAPTER 9 Introduction to SPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
SPL Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Iterator Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Iterator Helper Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Array Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
ArrayAccess Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Counting and ArrayAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
The Observer Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
SPL Autoloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Object Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
■CHAPTER 10 SPL Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Iterator Interfaces and Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Iterator Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Real-World Iterator Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Parsing XML with SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Accessing Flat-File Databases with DBA . . . . . . . . . . . . . . . . . . . . 159
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
■CHAPTER 11 SPL File and Directory Handling . . . . . . . . . . . . . . . . . . . . . . . . . 163
File and Directory Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Iteration of Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Listing Files and Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Finding Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Creating Custom File Filter Iterators . . . . . . . . . . . . . . . . . . . . . . . . . 169
McArthur_819-9FRONT.fm Page x Thursday, March 6, 2008 9:28 AM
■C ON TE N TS xi
SPL File Object Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
File Iteration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
CSV Operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Searching Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
■CHAPTER 12 SPL Array Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Introducing ArrayAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Introducing ArrayObject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Building an SPL Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Using Objects As Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
■CHAPTER 13 SPL Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Logic Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Runtime Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Bad Function and Method Call Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 192
Domain Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Range Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Invalid Argument Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Length Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Overflow Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Underflow Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
PART 4 ■ ■ ■ The Model-View-Controller (MVC) Pattern
■CHAPTER 14 MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Why Use MVC? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
MVC Application Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
From the Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Actions and Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
McArthur_819-9FRONT.fm Page xi Thursday, March 6, 2008 9:28 AM
xii ■CO N T E N T S
Criteria for Choosing an MVC Framework . . . . . . . . . . . . . . . . . . . . . . . . 204
Architecture of the MVC Framework. . . . . . . . . . . . . . . . . . . . . . . . . 204
MVC Framework Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
MVC Framework Community. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
MVC Framework Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
MVC Framework Flexibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Roll Your Own MVC Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Setting Up a Virtual Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Creating an MVC Framework. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
■CHAPTER 15 Introduction to the Zend Framework . . . . . . . . . . . . . . . . . . . . 215
Setting Up the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Installing the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Creating a Virtual Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Bootstrapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Creating Controllers, Views, and Models . . . . . . . . . . . . . . . . . . . . . . . . . 219
Adding an Index Controller. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Adding a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Defining Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Adding Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Using the Request and Response Objects . . . . . . . . . . . . . . . . . . . . 224
Using Built-in Action Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Using Built-in View Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
■CHAPTER 16 Advanced Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Managing Configuration Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The Array Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The INI Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
The XML Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Setting Site-Wide View Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Sharing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Application Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
McArthur_819-9FRONT.fm Page xii Thursday, March 6, 2008 9:28 AM
■C ON TE N TS xiii
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Caching Security Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Caching Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Authorizing Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Using JSON with PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Customizing Routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Managing Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Sending Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Creating PDF Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Creating New PDF Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Drawing on PDF Pages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Integrating with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
■CHAPTER 17 The Zend Framework Applied . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Module and Model Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Conventional Modular Directory Structure . . . . . . . . . . . . . . . . . . . 259
Model Libraries and Zend_Loader . . . . . . . . . . . . . . . . . . . . . . . . . . 260
The Request Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
Creating Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Creating Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Writing Action Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Writing View Helpers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Implementing Access Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Using a Two-Step View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Creating a Master Layout. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Using Placeholders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
PART 5 ■ ■ ■ Web 2.0 ■CHAPTER 18 Ajax and JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
JSON and PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
The JSON Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
JSON in the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
JSON and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
The XMLHttpRequest Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
McArthur_819-9FRONT.fm Page xiii Thursday, March 6, 2008 9:28 AM
xiv ■CO N T E N T S
Some Ajax Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
GET Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
POST Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
■CHAPTER 19 Introduction to Web Services with SOAP . . . . . . . . . . . . . . . . 285
Introduction to the PHP Web Services Architecture . . . . . . . . . . . . . . . . 285
Introduction to WSDL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
WSDL Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
A WSDL File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Introduction to SOAP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Using the PHP SOAP Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
SoapClient Class Methods and Options . . . . . . . . . . . . . . . . . . . . . . 291
SoapServer Class Methods and Options . . . . . . . . . . . . . . . . . . . . . 294
A Real-World Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
■CHAPTER 20 Advanced Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
A Complex Type Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Class Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
HTTP Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Communicated-Key Authentication . . . . . . . . . . . . . . . . . . . . . . . . . 306
Client-Certificate Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Sessions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Objects and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Binary Data Transmission . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
■CHAPTER 21 Certificate Authentication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Public Key Infrastructure Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Certificate Authority . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Web Server Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Client Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Root CA Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
McArthur_819-9FRONT.fm Page xiv Thursday, March 6, 2008 9:28 AM
■C ON TE N TS xv
Setting Up Client Certificate Authentication . . . . . . . . . . . . . . . . . . . . . . . 315
Creating Your Own Certificate Authority . . . . . . . . . . . . . . . . . . . . . . 315
Create a Self-Signed Web Server Certificate . . . . . . . . . . . . . . . . . . 317
Configuring Apache for SSL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Creating the Client-Side Certificates . . . . . . . . . . . . . . . . . . . . . . . . 320
Permitting Only Certificate Authentication . . . . . . . . . . . . . . . . . . . . 323
Testing the Certificate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
PHP Authentication Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Binding PHP to a Certificate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Setting Up Web Service Authentication . . . . . . . . . . . . . . . . . . . . . . 325
Just the Facts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
McArthur_819-9FRONT.fm Page xv Thursday, March 6, 2008 9:28 AM
McArthur_819-9FRONT.fm Page xvi Thursday, March 6, 2008 9:28 AM
xvii
About the Author
■KEVIN MCARTHUR is an open source developer, residing in British Columbia, Canada. He is a self-taught entrepreneur and has been running a very successful PHP application development studio for more than eight years. His company, StormTide Digital Studios, has worked with industry in the United States and Canada to provide scaling solutions for web statistics, VoIP, and print automation. An avid IRC user, Kevin helps to administer one of the largest PHP support organizations, PHP EFnet.
Kevin’s contributions to open source projects, including the Zend Framework, have made him a well-known authority in the industry. He has written several articles for PHPRiot.com on topics such as reflection, the Standard PHP Library, object-oriented programming, and PostgreSQL.
McArthur_819-9FRONT.fm Page xvii Thursday, March 6, 2008 9:28 AM
McArthur_819-9FRONT.fm Page xviii Thursday, March 6, 2008 9:28 AM
xix
About the Technical Reviewer
■JEFFREY SAMBELLS is a graphic designer and self-taught web application developer, best known for his unique ability to merge the visual world of graphics with the mental realm of code. After obtaining his Bachelor of Technology degree in graphic communications management with a minor in multimedia, Jeffrey enjoyed the paper-and-ink printing industry, but he soon realized the world of pixels and code was where his ideas would prosper.
Jeffrey has previously published articles related to print design and has contributed to award-winning graphical and Internet software designs. His latest book, AdvancED DOM Scripting (friends of ED, 2007; 1-59059-856-6, http://advanceddomscripting.com), was an instant success. In late 2005, Jeffrey became a PHP 4 Zend Certified Engineer. He updated the certification to PHP 5 in September 2006 to become one of the first PHP 5 Zend Certified Engineers! Jeffrey also maintains a blog at http://jeffreysambells.com, where he discusses his thoughts and ideas about every-thing from web development to photography.
Jeffrey currently lives and plays in Ontario, Canada, with his wife Stephanie, his daughter Addison, and their little dog Milo.
McArthur_819-9FRONT.fm Page xix Thursday, March 6, 2008 9:28 AM
McArthur_819-9FRONT.fm Page xx Thursday, March 6, 2008 9:28 AM
xxi
Acknowledgments
This book is based upon a decade of innovation by a community of thousands of developers. I would like to thank each of them, and offer this book as a modest contribution to our great work.
I would like to thank David Fugate for giving me the opportunity and guidance needed to write this book.
Thank you to Michael Geist, whose actions and advice have allowed me to achieve real change in the face of tremendous adversity.
I also have to thank my friends and family for their support. Without you, I could never have achieved so much.
Finally, to everyone at Apress who made this book possible, and who have helped many other writers to publish high-quality publications: you have my sincerest thanks. Without all of you, this book would never have been written.
McArthur_819-9FRONT.fm Page xxi Thursday, March 6, 2008 9:28 AM
McArthur_819-9FRONT.fm Page xxii Thursday, March 6, 2008 9:28 AM
xxiii
Introduction
Over the past decade, PHP has transformed itself from a set of simple tools for web site develop-ment to a full-fledged object-oriented programming (OOP) language. PHP now rivals mainstream languages like Java and C# for web application development, with more and more enterprises turning to it to power their web sites. The reasons for this are clear: PHP has found the right combination of an easy-to-learn language and powerful features.
In this book, you will learn how to make the most of your PHP programming, from a detailed understanding of OOP theory to frameworks and advanced system interoperability.
Who Should Read This BookThis is an advanced book. I have needed to choose carefully which information to include and what readers should be expected to know already. Readers should have a solid understanding of HTTP and PHP—that is, you should understand how to make web pages and build forms, and you should understand key concepts like the HTTP request cycle.
If this doesn’t sound like you, I recommend reading Beginning PHP and PostgreSQL 8 by Jason Gilmore and Robert Treat (Apress, 2006; 1-59059-547-3). It is an excellent introduction to PHP programming and a definite must-read for any would-be developer.
If you are comfortable at the intermediate to advanced level, then this book is for you.
How This Book Is OrganizedEach chapter builds on lessons learned in previous chapters, but recognizes that readers will have a wide variety of skill levels. If you think you already know the content covered in a chapter, I encourage you to skip ahead, but before you do, be sure to read the “Just the Facts” section at the end of each chapter. This section provides a terse summary of what was covered in the chapter. But note that even the most seasoned programmers are likely to find something worth learning in each chapter.
McArthur_819-9FRONT.fm Page xxiii Thursday, March 6, 2008 9:28 AM
xxiv ■IN TR O D U CT IO N
The book is organized into five parts:
Part 1, OOP and Patterns: This part provides a foundation for advanced OOP concepts. It dives right in and tells you all you need to know about abstract classes, interfaces, static methods, and patterns like the singleton and factory, as well as exceptions. The part concludes with an introduction to the new features in PHP 6.
Part 2, Testing and Documentation: This part covers all those interesting “peripheral” concepts, like test-driven development and automated deployment. It teaches you about writing great documentation and includes introductions to several documentation stan-dards, including PHPDoc and DocBook. You will find information about the reflection API and learn how to extract metadata from your programs. Finally, you’ll learn about contin-uous integration and how to use tools like Phing and Xinc to improve your development workflow.
Part 3, The Standard PHP Library (SPL): The SPL contains some of the most advanced PHP code ever written. It offers language support for advanced OOP concepts like indexers and iterators, and also provides structures for exceptions and patterns like observer/reporter. The information in this part will allow you to create much more elegant and well-formed classes than would normally be possible.
Part 4, The Model-View-Controller (MVC) Pattern: MVC is probably the most useful devel-opment pattern for PHP developers. It allows you to structure your applications and work in teams using the best resources to get the job done. A strong understanding of this pattern is probably the single most important job qualification for any PHP developer, so this book makes a special effort to fully explain it. This part of the book also introduces you to the Zend Framework, an MVC-based framework embraced by thousands of PHP companies. It starts with a complete walk-through of how to get a framework application up and running, and then presents the core concepts and advanced features of the Zend Framework.
Part 5, Web 2.0: This part covers all the things you need to know about Web 2.0. You will find information about Ajax and JSON, SOAP web services, and SSL client authentication. This part includes a lot of really useful tutorials, based on personal experience.
Contacting the AuthorPlease feel free to contact the author at [email protected]. You can find the latest information about this book at http://www.stormtide.ca/pro-php-book or on the Apress web site, http://www.apress.com/book/view/9781590598191. Last but not least, you can chat with the author via IRC by visiting #PHP EFnet.
McArthur_819-9FRONT.fm Page xxiv Thursday, March 6, 2008 9:28 AM