Skip to main content
main-content
Top

About this book

Aided by three key elements: object fundamentals, design principles, and best practices, you'll learn how to develop elegant and rock solid systems using PHP.

The 5th edition of this popular book has been fully updated for PHP 7, including replacing the PEAR package manager with Composer, and new material on Vagrant and PHP standards. It provides a solid grounding in PHP's support for objects, it builds on this foundation to instill core principles of software design and then covers the tools and practices needed to develop, test and deploy robust code.

PHP Objects, Patterns, and Practice begins by covering PHP's object-oriented features. It introduces key topics including class declaration, inheritance, reflection and much more.
The next section is devoted to design patterns. It explains the principles that make patterns powerful. The book covers many of the classic design patterns and includes chapters on enterprise and database patterns.
The last segment of the book covers the tools and practices that can help turn great code into a successful project. The section shows how to manage multiple developers and releases with git, how to manage builds and dependencies with Composer. It also explores strategies for automated testing and continuous integration.
What You'll LearnWork with object fundamentals: writing classes and methods, instantiating objects, creating powerful class hierarchies using inheritance.
Master advanced object-oriented features, including static methods and properties, managing error conditions with exceptions, and creating abstract classes and interfaces.
Learn about the new object-oriented features introduced by PHP 7 and why they matter for your code.
Understand and use design principles to deploy objects and classes effectively in your projects.
Discover a set of powerful patterns that you can deploy in your own projects.
Guarantee a successful project including unit testing; version control, build, installation and package management; and continuous integration.
Who This Book is For
This book is suitable for anyone with at least a basic knowledge of PHP who wants to use its object-oriented features in their projects.
Those who already know their interfaces from their abstracts may well still find it hard to use these features in their systems. They will benefit from the book's emphasis on design. They will learn how to choose and combine the participants of a system; how to read design patterns and how to use them in their code.
Finally this book is for PHP coders who want to learn about the practices and tools (version control, testing, continuous integration, etc) that can make projects safe, elegant and stable.

Table of Contents

Objects

Frontmatter

Chapter 1. PHP: Design and Management

Abstract
In July 2004 PHP 5.0 was released. This version introduced a suite of radical enhancements. Perhaps first among these was radically improved support for object-oriented programming. This stimulated much interest in objects and design within the PHP community. In fact, this was an intensification of a process that began when version 4 first made object-oriented programming with PHP a serious reality.
Matt Zandstra

Chapter 2. PHP and Objects

Abstract
Objects were not always a key part of the PHP project. In fact, they were once described as an afterthought by PHP’s designers.
Matt Zandstra

Chapter 3. Object Basics

Abstract
Objects and classes lie at the heart of this book and, since the introduction of PHP 5 over a decade ago, they have lain at the heart of PHP, too. In this chapter, I lay down the groundwork for more in-depth coverage of objects and design by examining PHP’s core object-oriented features. If you are new to object-oriented programming, you should read this chapter carefully.
Matt Zandstra

Chapter 4. Advanced Features

Abstract
You have already seen how class type hinting and access control give you more control over a class’s interface. In this chapter, I will delve deeper into PHP’s object-oriented features.
Matt Zandstra

Chapter 5. Object Tools

Abstract
As we have seen, PHP supports object-oriented programming through language constructs such as classes and methods. The language also provides wider support through functions and classes designed to help you work with objects.
Matt Zandstra

Chapter 6. Objects and Design

Abstract
Now that we have seen the mechanics of PHP’s object support in some detail, we will step back from the details and consider how best to use the tools that we have encountered. In this chapter, I introduce you to some of the issues surrounding objects and design. I will also look at the UML, a powerful graphical language for describing object-oriented systems.
Matt Zandstra

Patterns

Frontmatter

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

Abstract
Most problems we encounter as programmers have been handled time and again by others in our community. Design patterns can provide us with the means to mine that wisdom. Once a pattern becomes a common currency, it enriches our language, making it easy to share design ideas and their consequences. Design patterns simply distill common problems, define tested solutions, and describe likely outcomes. Many books and articles focus on the details of computer languages, such as the available functions, classes and methods, and so on. Pattern catalogs concentrate instead on how you can move on from these basics (the “what”) to an understanding of the problems and potential solutions in your projects (the “why” and “how”).
Matt Zandstra

Chapter 8. Some Pattern Principles

Abstract
Although design patterns simply describe solutions to problems, they tend to emphasize solutions that promote reusability and flexibility. To achieve this, they manifest some key object-oriented design principles. We will encounter some of them in this chapter and in more detail throughout the rest of the book.
Matt Zandstra

Chapter 9. Generating Objects

Abstract
Creating objects is a messy business. So, many object-oriented designs deal with nice, clean abstract classes, taking advantage of the impressive flexibility afforded by polymorphism (the switching of concrete implementations at runtime). To achieve this flexibility, though, I must devise strategies for object generation. This is the topic I will look at in this chapter.
Matt Zandstra

Chapter 10. Patterns for Flexible Object Programming

Abstract
With strategies for generating objects covered, we’re free now to look at some strategies for structuring classes and objects. I will focus in particular on the principle that composition provides greater flexibility than inheritance. The patterns I examine in this chapter are once again drawn from the Gang of Four catalog.
Matt Zandstra

Chapter 11. Performing and Representing Tasks

Abstract
In this chapter, we get active. I look at patterns that help you to get things done, whether interpreting a mini-language or encapsulating an algorithm.
Matt Zandstra

Chapter 12. Enterprise Patterns

Abstract
In July 2004 PHP 5.0 was released. This version introduced a suite of radical enhancements. Perhaps first among these was radically improved support for object-oriented programming. This stimulated much interest in objects and design within the PHP community. In fact, this was an intensification of a process that began when version 4 first made object-oriented programming with PHP a serious reality.
Matt Zandstra

Chapter 13. Database Patterns

Abstract
Most web applications of any complexity handle persistence to a greater or lesser extent. Shops must recall their products and their customer records. Games must remember their players and the state of play. Social networking sites must keep track of your 238 friends and your unaccountable liking for boy-bands of the ’80s and ’90s. Whatever the application, the chances are it’s keeping score behind the scenes. In this chapter, I look at some patterns that can help.
Matt Zandstra

Practice

Frontmatter

Chapter 14. Good (and Bad) Practice

Abstract
So far in this book, I have focused on coding, concentrating particularly on the role of design in building flexible and reusable tools and applications. Development doesn’t end with code, however. It is possible to come away from books and courses with a solid understanding of a language, yet still encounter problems when it comes to running and deploying a project.
Matt Zandstra

Chapter 15. PHP Standards

Abstract
Unless you are a lawyer or a health inspector, the topic of standards probably does not make your heart race. However, what standards help us achieve is worth getting excited about. Standards promote interoperability, and that gives us access to a vast array of compatible tools and framework components.
Matt Zandstra

Chapter 16. PHP Using and Creating Components with Composer

Abstract
Programmers aspire to produce reusable code. This is one of the great goals in object-oriented coding. We like to abstract useful functionality from the messiness of specific context, turning a particular solution into a tool that can be used again and again. To come at this from another angle, if programmers love the reusable, they hate duplication. By creating libraries that can be reapplied, programmers avoid the need to implement similar solutions across multiple projects.
Matt Zandstra

Chapter 17. Version Control with Git

Abstract
All disasters have their tipping point, the moment at which order finally breaks down and events simply spiral out of control. Do you ever find yourself in projects like that? Are you able to spot that crucial moment?
Matt Zandstra

Chapter 18. Testing with PHPUnit

Abstract
Every component in a system depends for its continued smooth running on the consistency of operation and interface of its peers. By definition, then, development breaks systems. As you improve your classes and packages, you must remember to amend any code that works with them. For some changes, this can create a ripple effect, affecting components far away from the code you originally changed. Eagle-eyed vigilance and an encyclopedic knowledge of a system’s dependencies can help to address this problem. Of course, while these are excellent virtues, systems soon grow too complex for every unwanted effect to be easily predicted, not least because systems often combine the work of many developers. To address this problem, it is a good idea to test every component regularly. This, of course, is a repetitive and complex task; and as such, it lends itself well to automation.
Matt Zandstra

Chapter 19. Automated Build with Phing

Abstract
If version control is one side of the coin, then automated build is the other. Version control allows multiple developers to work collaboratively on a single project. With many coders each deploying a project in her own space, automated build soon becomes essential. One developer may have her web-facing directory in /usr/local/apache/htdocs; another might use /home/bibble/public_html. Developers may use different database passwords, library directories, or mail mechanisms. A flexible codebase might easily accommodate all of these differences, but the effort of changing settings and manually copying directories around your file system to get things working would soon become tiresome—especially if you need to install code in progress several times a day (or several times an hour).
Matt Zandstra

Chapter 20. Vagrant

Abstract
Maybe you have a development environment you have honed to perfection with a favorite editor and any number of useful development tools. Of course, your perfect set up for writing code is probably very different from the best system on which to run it. And that’s a challenge that Vagrant can help you with. Using Vagrant you get to work on your local machine and run your code on a system that’s all but identical to your production server. In this chapter I will show you how.
Matt Zandstra

Chapter 21. Continuous Integration

Abstract
In previous chapters, you’ve seen a plethora of tools that are designed to support a well-managed project. Unit testing, documentation, build, and version control are all fantastically useful. But tools, and testing in particular, can be bothersome.
Matt Zandstra

Chapter 22. Objects, Patterns, Practice

Abstract
From object basics through design pattern principles, and on to tools and techniques, this book has focused on a single objective: the successful PHP project.
Matt Zandstra

Chapter 23. Appendix A: Bibliography

Abstract
Alexander, Christopher, Sara Ishikawa, Murray Silverstein, Max Jacobson, Ingrid Fiksdahl-King, and Shlomo Angel. A Pattern Language: Towns, Buildings, Construction. Oxford, UK: Oxford University Press, 1977.
Matt Zandstra

Chapter 24. Appendix B: A Simple Parser

Abstract
The Interpreter pattern discussed in Chapter 11 does not cover parsing. An interpreter without a parser is pretty incomplete, unless you persuade your users to write PHP code to invoke the interpreter! Third-party parsers are available that could be deployed to work with the Interpreter pattern, and that would probably be the best choice in a real-world project.
Matt Zandstra
Additional information