In July 2004, PHP 5.0 was released. This version introduced a suite of radical enhancements, notably improved support for object-oriented programming. This sparked significant interest in objects and design within the PHP community, continuing a trend that began with PHP 4, which first made object-oriented programming with PHP a serious reality.

In this chapter, I examine some of the needs that coding with objects can address, briefly summarizing aspects of the evolution of patterns and related practices. I also outline the topics covered by this book, including:

  • The evolution of disaster: A project goes bad
  • Design and PHP: How object-oriented design techniques took root in the PHP community
  • This book: Objects Patterns Practice

The Problem

PHP’s ease of use tempts you to try out your ideas and flatters you with good results. You often write code directly into your web pages, adding utility functions (such as database access code) to files that can be included from page to page. Before long, you have a working web application.

However, trouble strikes when you begin a new phase. With a larger team, more users, and a bigger budget, things start to go wrong. A new programmer struggles to understand your code, simple changes take longer than expected, and file version conflicts arise. Server migrations reveal hard-coded file paths, database names, and passwords, causing delays.

Bugs emerge soon after deployment, often due to missed modifications or merge collisions. Last-minute changes can introduce new problems, leading to downtime and lost revenue.

This common tale highlights the challenges of maintaining PHP projects as they grow. Presentation layers containing application logic lead to code duplication, making changes laborious and error-prone. Lack of documentation and testing allows bugs to go undiscovered until deployment. As code evolves away from its original purpose, it becomes difficult to maintain or rewrite parts of it.

While this can be lucrative for freelance PHP consultants, such problems can mean the difference between a business’s success and failure.

PHP and Other Languages

PHP’s popularity tested its boundaries early. Originally a set of macros for managing personal home pages, PHP evolved with versions 3 and 4 to power large enterprise websites. Despite its success, PHP retained a reputation as a hobbyist language best suited for presentation tasks.

Around the turn of the millennium, new ideas gained traction in other coding communities. Object-oriented design galvanized the Java community, with design patterns providing a way to describe problems and solutions in software design. The concept originated in architecture, popularized by Christopher Alexander’s A Pattern Language (1977). By the early 1990s, object-oriented programmers used design patterns to describe software design problems. The seminal book Design Patterns: Elements of Reusable Object-Oriented Software (1995) by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides (the “Gang of Four”) remains indispensable today.

Design patterns promoted software design, with related topics like Extreme Programming (XP) gaining prominence. XP, championed by Kent Beck, emphasized testing, flexible design, and highly focused planning and execution. Automated testing and iterative development became central to XP, with principles like code revision (refactoring) and tools like JUnit test platform becoming key.

PHP 4’s release brought improved efficiency and enhanced object support, making fully object-oriented projects possible. Despite imperfect object support, PHP programmers embraced objects, spurred by improved object support in PHP 5’s Zend Engine 2. Online interest in PHP and patterns grew, leading to pattern-based framework projects and tools for automated testing and documentation.

PHP 5 continued to evolve, incorporating important new features and solidifying PHP’s reputation as a top choice for server-side web programming. PHP 7, released in December 2015, introduced significant features like type declarations, anonymous classes, improved memory usage, and increased speed. PHP 8, expected in December 2020, includes improvements to type declarations, streamlined property assignment, and support for attributes.

About This Book

This book does not aim to break new ground in object-oriented design but examines well-established design principles and key patterns in the context of PHP. It is divided into three main parts: objects, patterns, and practice.

Objects

Part 1 begins with a history of PHP and objects, from their afterthought status in PHP 3 to core feature in PHP 5. Even experienced PHP programmers with little knowledge of objects can benefit from an introduction to objects, classes, and inheritance. Advanced object-oriented features in PHP 5, 7, and 8 are also covered, along with tools to work with objects and classes. The section concludes with an introduction to object-oriented design with PHP.

Patterns

Part 2 defines design patterns and their structure, discussing their popularity and core design principles. The Unified Modeling Language (UML) is introduced as a platform-independent way of describing classes and interactions. Some of the most famous and useful patterns are examined, describing the problems they address, analyzing solutions, and presenting PHP implementation examples.

Practice

Part 3 looks at tools for managing a PHP codebase, from collaboration through deployment. Topics include automated testing, dependency management with Composer, version control with Git, PHP standards, continuous integration, and deployment tools like Phing and Vagrant. Tools for automated builds and tests, essential for maintaining code quality, are also discussed.

What’s New in the Sixth Edition

PHP is a living language, and this new edition has been thoroughly updated to reflect changes and new opportunities. New features such as attributes and enhancements to type declarations are covered, with examples using PHP 8 features where appropriate.

Summary

This book addresses object-oriented design and programming, as well as tools for managing a PHP codebase. It aims to build systems that achieve their objectives and support collaborative development. A secondary goal is the aesthetics of software systems, emphasizing clean, clever code, and well-designed codebases for developers and users alike.