You are previewing PHP Objects, Patterns, and Practice, Fourth Edition.
O'Reilly logo
PHP Objects, Patterns, and Practice, Fourth Edition

Book Description

PHP Objects Patterns and Practice, Fourth Edition is revised and updated throughout. The book begins by covering PHP's object-oriented features. It introduces key topics including class declaration, inheritance, reflection and much more. These provide the fundamentals of the PHP's support for objects. It also introduces some principles of design. This edition introduces new object relevant features such as traits, reflection extension additions, callable type hinting, improvements to exception handling, and many smaller language enhancements.

The next section is devoted to design patterns. These describe common problems and their solutions. The section describes the design principles that make patterns powerful. It 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 build and install using Phing and PEAR. It also explores strategies for automated testing and build.

In addition to discussing the latest developments in build, test, and continuous integration, this section keeps pace with best practice in version control by focusing on Git, increasingly the developer's system of choice.

Taken together these three elements: object fundamentals, design principles, and best practice will help the reader develop elegant and rock solid systems.

PHP Objects and Patterns:

  • Describes and demonstrates PHP's built-in object-oriented features

  • Breaks down the principles of object-oriented design, explaining key design patterns using practical examples.

  • Discusses the tools and practices necessary for developing, testing and deploying exemplary applications.

What you'll learn

You will:

  • Learn to work 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.

  • Understand and use design principles to deploy objects and classes effectively in your projects.

  • Learn about design patterns, their purpose, structure and the underlying principles that govern them.

  • Discover a set of powerful patterns that you can deploy in your own projects

  • Learn about the tools and practices that can 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 projects. 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

  1. Title Page
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Author
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Introduction
  9. CHAPTER 1: PHP: Design and Management
    1. The Problem
    2. PHP and Other Languages
    3. About This Book
    4. Summary
  10. CHAPTER 2: Objects
    1. The Accidental Success of PHP Objects
    2. Advocacy and Agnosticism: The Object Debate
    3. Summary
  11. CHAPTER 3: Object Basics
    1. Classes and Objects
    2. Setting Properties in a Class
    3. Working with Methods
    4. Arguments and Types
    5. Inheritance
    6. Summary
  12. CHAPTER 4: Advanced Features
    1. Static Methods and Properties
    2. Constant Properties
    3. Abstract Classes
    4. Interfaces
    5. Traits
    6. Late Static Bindings: The static Keyword
    7. Handling Errors
    8. Final Classes and Methods
    9. Working with Interceptors
    10. Defining Destructor Methods
    11. Copying Objects with __clone(  )
    12. Defining String Values for Your Objects
    13. Callbacks, Anonymous Functions and Closures
    14. Summary
  13. CHAPTER 5: Object Tools
    1. PHP and Packages
    2. The Class and Object Functions
    3. The Reflection API
    4. Summary
  14. CHAPTER 6: Objects and Design
    1. Defining Code Design
    2. Object-Oriented and Procedural Programming
    3. Choosing Your Classes
    4. Polymorphism
    5. Encapsulation
    6. Forget How to Do It
    7. Four Signposts
    8. The UML
    9. Summary
  15. CHAPTER 7: What Are Design Patterns? Why Use Them?
    1. What Are Design Patterns?
    2. A Design Pattern Overview
    3. The Gang of Four Format
    4. Why Use Design Patterns?
    5. PHP and Design Patterns
    6. Summary
  16. CHAPTER 8: Some Pattern Principles
    1. The Pattern Revelation
    2. Composition and Inheritance
    3. Decoupling
    4. Code to an Interface, Not to an Implementation
    5. The Concept That Varies
    6. Patternitis
    7. The Patterns
    8. Summary
  17. CHAPTER 9: Generating Objects
    1. Problems and Solutions in Generating Objects
    2. The Singleton Pattern
    3. Factory Method Pattern
    4. Abstract Factory Pattern
    5. Prototype
    6. But That’s Cheating!
    7. Summary
  18. CHAPTER 10: Patterns for Flexible Object Programming
    1. Structuring Classes to Allow Flexible Objects
    2. The Composite Pattern
    3. The Decorator Pattern
    4. The Facade Pattern
    5. Summary
  19. CHAPTER 11: Performing and Representing Tasks
    1. The Interpreter Pattern
    2. The Strategy Pattern
    3. The Observer Pattern
    4. The Visitor Pattern
    5. The Command Pattern
    6. Summary
  20. CHAPTER 12: Enterprise Patterns
    1. Architecture Overview
    2. Cheating Before We Start
    3. The Presentation Layer
    4. The Business Logic Layer
    5. Summary
  21. CHAPTER 13: Database Patterns
    1. The Data Layer
    2. Data Mapper
    3. Identity Map
    4. Unit of Work
    5. Domain Object Factory
    6. The Identity Object
    7. The Selection Factory and Update Factory Patterns
    8. What’s Left of Data Mapper Now?
    9. Summary
  22. CHAPTER 14: Good (and Bad) Practice
    1. Beyond Code
    2. Borrowing a Wheel
    3. Playing Nice
    4. Giving Your Code Wings
    5. Documentation
    6. Testing
    7. Continuous Integration
    8. Summary
  23. CHAPTER 15: An Introduction to PEAR and Pyrus
    1. What Is PEAR?
    2. Phar Out with Pyrus
    3. Installing a Package
    4. Using a PEAR Package
    5. Creating Your Own PEAR Package
    6. Summary
  24. CHAPTER 16: Generating Documentation with phpDocumentor
    1. Why Document?
    2. Installation
    3. Generating Documentation
    4. DocBlock Comments
    5. Documenting Classes
    6. File-Level Documentation
    7. Documenting Properties
    8. Documenting Methods
    9. Namespace support
    10. Creating Links in Documentation
    11. Summary
  25. CHAPTER 17: Version Control with Git
    1. Why Use Version Control?
    2. Getting Git
    3. Configuring a Git Server
    4. Beginning a Project
    5. Updating and Committing
    6. Adding and Removing Files and Directories
    7. Tagging a Release
    8. Branching a Project
    9. Summary
  26. CHAPTER 18: Testing with PHPUnit
    1. Functional Tests and Unit Tests
    2. Testing by Hand
    3. Introducing PHPUnit
    4. Writing Web Tests
    5. A Note of Caution
    6. Summary
  27. CHAPTER 19: Automated Build with Phing
    1. What Is Phing?
    2. Getting and Installing Phing
    3. Composing the Build Document
    4. Summary
  28. CHAPTER 20: Continuous Integration
    1. What Is Continuous Integration?
    2. Jenkins
    3. Summary
  29. CHAPTER 21: Objects, Patterns, Practice
    1. Objects
    2. Patterns
    3. Practice
    4. Summary
  30. APPENDIX A: Bibliography
    1. Books
    2. Articles
    3. Sites
  31. APPENDIX B: A Simple Parser
    1. The Scanner
    2. The Parser
  32. Index