Learning PHP Design Patterns
William Sanders
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Dedication:
In memory of my father, William B. Sanders (19172012).
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
As PHP expands to become the server-side program of choice among a significant portion of programmers, professional techniques and programming structures need to be incorporated. Design patterns , a concept borrowed from The Timeless Way of Building by Christopher Alexander (Oxford University Press), refers to a general reusable solution to a commonly occurring problem within a given context . In everyday development work, PHP programmers encounter commonly occurring problems in a software development context, and the use of PHP design patterns is a set of solutions to commonly occurring PHP programming problems. Pure and simple, PHP design patterns are tools used to deal with the reality of professional software development.
They are not libraries or templates but rather more general structures that can be used to solve problems. I like to think of design patterns in the same way as I do the loop structure. The loop is used when some kind of iteration is required. True, there are other ways to deal with iteration, but a loop is a flexible tool that saves time in the development process. (Besides, its a lot neater than writing the same line of code 10,000 times!)
Further, I do not feel constrained by design patterns as canned solutions any more than a loop is a canned solution to iteration. Besides, I can use the loop structure in a variety of ways in PHP ranging from for
to while
statements and everything in between. Likewise, design patterns can be implemented in more than one way depending on the exact nature of the problem being solved.
The most important reason for incorporating design patterns, though, is that they provide solutions to complex problems. As ones programs become larger, they almost necessarily become more complex. In an object-oriented programming (OOP) environment, such complexity is reduced somewhat, as you are dealing with encapsulated modules, whereas in sequential or procedural programming, any changes can bring the program crashing down like a house of cards. Design patterns bring not only solutions to general programming problems, but they also allow changes to be made in large complex programs by providing loose coupling between objects. So when a change is made, instead of having to start programming all over from scratch, you can just add the necessary changes and everything keeps chugging alongeven in large, complex programs.
Further, design patterns are meant for reuse. After all, programmers reuse the same algorithms all the time. Why not use larger structures as well? On the one hand, frameworks and templates serve to make reuse practical, but they are often too specific. Thats where reuse of design-patterned PHP programs comes in, especially in the case of large, complex programs. Because making changes is easy with design patterns, reuse for specific problems of the same kind is easy as well. Reducing developmental time and resources saves money and better serves your clients. They get well-structured programs that do what the clients want, theyre easy for the developers to change (customers always want change!), and they have strong foundations that are not as likely to fail.
Audience
At some point, all good programmers realize that they need to get out of a sequential and procedural programming rut. The next logical step is object-oriented programming, and moving into OOP requires a shift in perspective: instead of seeing programming as a series of statements, it must be seen as an interaction and communication between objects. Beyond OOP lie design patterns, where OOP principles are recast into patterns of reusable code. There you will find the tools of professional programmers. Because design patterns for programming were developed in cooperation between academics and businesses, the concepts both transcend single problems while at the same time possessing business-like practicality. Learning PHP design patterns is for professional programmers who want to optimize their own time in development and redevelopment and provide their clients with high-quality code.
In no small measure, this book is for those who felt a certain delight when programming was new to them. This is for the developer who thought nothing of working on a program all night long just because it was interesting, staggering off to bed, only to start on another program as soon as he woke up. When programming is new and every day promises a new discovery or a bug that challenges the developer to a fight to the death, and you must use your mind in complex and exciting ways, the experience can be Zen-like. If youve had that experience, youll know what I mean. It cannot be spelled out or explained analytically. (I cant even explain it to myself, and I have no idea why I enjoy the kinds of challenges and rewards found in programming.)
Design patterns reintroduce mental challenges, and this book is not for someone who is new either to PHP or to programming. If youre learning PHP for the first time, take a look at Robin Nixons book, Learning PHP, MySQL, JavaScript, and CSS, 2nd Edition (OReilly) before tackling PHP design patterns. Likewise, this book (or any decent book on design patterns) does not promise that youll master design patterns quickly and easily. This kind of learning is a journey, and the wisest counsel is to learn to enjoy the journey. It takes time and mental effort.
Assumptions This Book Makes
This book assumes that you know how to program in PHP and want to take your programming skills to the next couple of levels. In fact, it assumes that youre a pretty good PHP programmer and youve used MySQL and know how to develop HTML pages and use CSS. It also assumes that you understand that learning PHP design patterns is not going to happen in a lazy afternoon. Learning design patterns is akin to a gradual metamorphosis.
Contents of This Book
This book is organized into five parts.
is an OOP refresher/introduction:
introduces object-oriented programming (OOP) and how to more easily handle complex programming problems with modularization.
discusses basic concepts in OOP such as abstraction, encapsulation, inheritance, and polymorphism, as well as the PHP structures that implement these concepts.
moves on to examine the basic concepts in design patterns, their categorization, and how specific patterns are selected to handle specific problems.
introduces Unified Modeling Language (UML) and explains how it will be employed in this book.
covers creational design patterns:
examines the Factory Method, which has a creational purpose and a class scope. Examples include dynamically creating pages that display graphics, body text, and header text.