This book’s purpose involves a kind of bigamy. It introduces state-of-the art object-oriented design principles, patterns, and techniques. Then it weds these to two different partners. The first partner is PHP, the programming language. The second partner is the PHP programmer’s everyday work.
More specifically, this book is about handling and implementing these principles, patterns, and techniques in PHP with its specific syntax and characteristics. It is also about how to apply them to the specific and common challenges of web programming.
This book is for programmers who develop applications in PHP and want to learn modern object-oriented practices, principles, and techniques, and how to apply them to the everyday challenges of web programming.
It is not a beginner’s book in PHP; it presupposes a minimum of familiarity with PHP—or experience in other programming languages—and with the basic ideas and challenges of web programming.
The book is divided into four parts. Parts 1 and 2 introduce the principles, patterns, and techniques mentioned initially and demonstrate how they can be implemented in PHP. Part 1 introduces and develops the subjects of object-oriented programming and design. Part 2 deals with unit testing and refactoring.
Parts 3 and 4 apply the material from the first two parts to the everyday challenges of web programming. Part 3 is about the web interface, while part 4 deals with databases and data storage.
Part 1 moves gradually, chapter by chapter, from the nuts and bolts of object-oriented programming in PHP to the more conceptual subject of object-oriented application design.
Chapter 1 introduces and discusses the pros and cons of PHP and agile practices.
Chapter 2 and chapter 3 deal with the mechanics and syntax of object-oriented programming in PHP. Although objects and classes are ultimately inseparable subjects, chapter 2 focuses mostly on object features and chapter 3 on class features.
Chapter 4 discusses why objects and classes are a good idea, how they relate to the real world, and how we can tell the difference between good and bad object-oriented designs.
Chapter 5 is about the basic class relationships—inheritance, association, and composition—and the role of interfaces in program design.
Chapter 6 is where we start to go into object-oriented design in earnest. It deals with object-oriented principles that serve as general guidelines for design.
Chapter 7 introduces the subject of design patterns—recurrent solutions to common design problems—and describes some of the most common ones.
Chapter 8 shows how design principles and patterns work in the context of an extended example: date and time handling.
Part 2 focuses on testing and refactoring (improving the design of existing code) from two perspectives: as quality assurance, and as a learning process.
Chapter 9 introduces unit testing and test-driven development, using a database transaction class as an example.
Chapter 10 digs deeper into the realm of unit testing, showing how to set up tests properly and use mock objects and other fakes to make testing easier. It builds on the previous example by creating a contact manager on top of the transaction class.
Chapter 11 is about refactoring, with a particular focus on web applications. It deals with refactoring in the traditional object-oriented sense as well as techniques for getting poorly designed procedural code into a more manageable state.
Chapter 12 finishes the subject of testing by moving the searchlight from unit testing to web testing. Using the contact manager once again, it shows how to make sure the user interface is what the customer wanted and how to design the entire web application top-down.
Part 3 is about the defining feature of web programming: the web interface.
Chapter 13 explains the principles of separating HTML markup from program code, and describes how this can be done by using template engines and specific techniques.
Chapter 14 takes on the challenge of assembling web pages from many separate components and tells you how to implement the Composite View design pattern.
Chapter 15 introduces the subject of user interaction and the Model-View-Controller (MVC) design pattern.
Chapter 16 teaches you how to implement the web-specific variations on MVC, including Page Controller and Front Controller.
Chapter 17 deals in depth with server-side and client-side input validation and how to synchronize these.
Chapter 18 shows how to develop form handling, building on the PEAR package HTML_QuickForm.
Part 4 deals with the subject of databases and data storage from an object-oriented point of view.
Chapter 19 tells two different stories. One is about how to handle database connections appropriately in an object-oriented application and how to deal with the configuration the database connection requires. The other is about database abstraction: how to make the code independent of the specifics of one database management system.
Chapter 20 is about the challenges posed by the fact that we have to use a completely separate programming language—SQL—to query the database. It shows how to encapsulate, hide, and generalize SQL code.
Chapter 21 assembles some of the pieces from the two previous chapters into complete design patterns for object-oriented data access.
Appendix A gives some specific information on testing and test tools that did not fit into the chapters on testing. Reference material on the essential parts of the SimpleTest and PHPUnit APIs is included.
Appendix B is an introduction to security in PHP.
The parts of this book are relatively independent. It should be possible to start reading any one of them without reading the earlier parts. Unless you already have a strong grasp of object-oriented programming and design, reading part 1 first is likely to make your understanding of part 3 and part 4 easier, deeper, and more complete. But the workings of all the examples in the later parts are explained in detail. The examples throw light on the concepts from part 1, but generally do not depend on them.
On the other hand, some of the chapters in each part depend heavily on each other. For example, it may be difficult to read the refactoring examples in chapter 11 without understanding the basics of unit testing as explained in chapters 9 and 10.
All source code in listings or in text is in a fixed-width font like this to separate it from ordinary text. Annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.
Source code for all of the working examples in this book is available for download from www.manning.com/reiersol or www.manning.com/PHPinAction.
Purchase of PHP in Action includes free access to a private web forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and from other users. To access the forum and subscribe to it, point your web browser to www.manning.com/reiersol or www.manning.com/PHPinAction. This page provides information on how to get on the forum once you are registered, what kind of help is available, and the rules of conduct on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful dialog between individual readers and between readers and the authors can take place. It is not a commitment to any specific amount of participation on the part of the authors, whose contribution to the AO remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray! The Author Online forum and the archives of previous discussions will be accessible from the publisher's website as long as the book is in print.
Dagfinn Reiersøl has been designing and developing web applications, web content mining software, web programming tools, and text analysis programs, mostly in PHP, since 1997. He also has a long history as a technical writer of software manuals. He lives in Oslo, Norway.
Marcus Baker has been a software consultant for many years specializing in OO design and development as well as web application development and testing. He is also a columnist for PHP Architecture Magazine and lives in London, England.
Chris Shiflett is a PHP consultant and security expert as well as a leader in the PHP community. He is the founder of the PHP Security Consortium and the author of the HTTP Developer’s Handbook and Essential PHP Security. He lives in Brooklyn, New York.