You are previewing Learning PHP Design Patterns.
O'Reilly logo
Learning PHP Design Patterns

Book Description

Build server-side applications more efficiently—and improve your PHP programming skills in the process—by learning how to use design patterns in your code. This book shows you how to apply several object-oriented patterns through simple examples, and demonstrates many of them in full-fledged working applications.

Table of Contents

  1. Dedication:
  2. Special Upgrade Offer
  3. Preface
    1. Audience
    2. Assumptions This Book Makes
    3. Contents of This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  4. I. Easing into the Fundamentals of Design Patterns
    1. 1. PHP and Object-Oriented Programming
      1. Entering into Intermediate and Advanced Programming
      2. Why Object-Oriented Programming?
        1. Making Problem Solving Easier
        2. Modularization
      3. Classes and Objects
        1. Single Responsibility Principle
        2. Constructor Functions in PHP
      4. The Client as a Requester Class
      5. What About Speed?
        1. The Speed of Development and Change
        2. The Speed of Teams
      6. What’s Wrong with Sequential and Procedural Programming?
        1. Sequential Programming
        2. Procedural Programming
        3. Pay Me Now or Pay Me Later
    2. 2. Basic Concepts in OOP
      1. Abstraction
        1. Abstract Classes
        2. Abstract Properties and Methods
        3. Interfaces
        4. Interfaces and Constants
        5. Type Hinting: Almost Data Typing
      2. Encapsulation
        1. Everyday Encapsulation
        2. Protecting Encapsulation through Visibility
          1. Private
          2. Protected
          3. Public
        3. Getters and Setters
      3. Inheritance
      4. Polymorphism
        1. One Name with Many Implementations
        2. Built-In Polymorphism in Design Patterns
      5. Easy Does It
    3. 3. Basic Design Pattern Concepts
      1. The MVC Loosens and Refocuses Programming
      2. Basic Principles of Design Patterns
        1. The First Design Pattern Principle
        2. Using Interface Data Types in Code Hinting
        3. Abstract Classes and Their Interfaces
        4. The Second Design Pattern Principle
        5. Basic Composition Using a Client
        6. Delegation: The IS-A and HAS-A Difference
      3. Design Patterns as a Big Cheat Sheet
        1. Organization of Design Patterns
          1. Creational patterns
          2. Structural patterns
          3. Behavioral patterns
          4. Class category
          5. Object category
      4. Choosing a Design Pattern
        1. What Causes Redesign?
        2. What Varies?
        3. What Is the Difference Between Design Patterns and Frameworks?
    4. 4. Using UMLs with Design Patterns
      1. Why Unified Modeling Language (UML)?
      2. Class Diagrams
      3. Participant Symbols
      4. Relationship Notations
        1. Acquaintance Relations
        2. Aggregation Relationship
        3. Inheritance and Implementation Relations
        4. Creates Relations
        5. Multiple Relations
      5. Object Diagrams
      6. Interaction Diagrams
      7. The Role of Diagrams and Notations in Object-Oriented Programming
      8. Tools for UMLs
      9. Other UMLs
  5. II. Creational Design Patterns
    1. 5. Factory Method Design Pattern
      1. What Is the Factory Method Pattern?
      2. When to Use the Factory Method
      3. A Minimalist Example
        1. Factory Work
          1. The Product
        2. The Client
      4. Accommodating Class Changes
        1. Adding Graphic Elements
        2. Coordinating Products
        3. Changing the Text Product
        4. Changing the Graphic Product
        5. Adding New Products and Parameterized Requests
        6. One Factory and Multiple Products
        7. The New Factories
        8. The New Products
        9. The Client with Parameters
        10. Helper Classes
        11. File Diagram
        12. Product Changes: Leave the Interface Alone!
    2. 6. Prototype Design Pattern
      1. What Is the Prototype Design Pattern?
      2. When to Use the Prototype Pattern
      3. The Clone Function
        1. Constructor Does Not Relaunch with Clone
        2. The Constructor Function Should Do No Real Work
      4. A Minimalist Prototype Example
        1. Studying Fruit Flies
          1. The abstract class interface and concrete implementation
          2. The Client
      5. Adding OOP to the Prototype
        1. The Modern Business Organization
        2. Encapsulation in the Interface
        3. The Interface Implementations
        4. The Organizational Client
        5. Making Changes, Adding Features
        6. Dynamic Object Instantiation
          1. Variables to objects
      6. The Prototype in PHP Land
  6. III. Structural Design Patterns
    1. 7. The Adapter Pattern
      1. What Is the Adapter Pattern?
      2. When to Use the Adapter Pattern
      3. The Adapter Pattern Using Inheritance
        1. A Minimal Example of a Class Adapter: The Currency Exchange
          1. Enter the euro
          2. Creating a euro adapter
      4. The Adapter Pattern Using Composition
        1. From Desktop to Mobile
          1. Just the desktop
            1. Adapting to mobile
          2. The Client class as participant
        2. Adapters and Change
    2. 8. Decorator Design Pattern
      1. What Is the Decorator Pattern?
      2. When to Use the Decorator Pattern
      3. Minimalist Decorator
        1. The Component Interface
        2. The Decorator Interface
        3. Concrete Component
        4. Concrete Decorators
          1. Maintenance
          2. Video
          3. Database
        5. The Client
      4. What About Wrappers?
        1. Primitives in Wrappers
        2. Built-in Wrappers in PHP
        3. Design Pattern Wrappers
      5. Decorators with Multiple Components
        1. Multiple Concrete Components
        2. Concrete Decorators with Multiple States and Values
        3. The Developer Dating Service
          1. Component interface
          2. Concrete components
          3. Decorator with component methods
          4. Concrete decorators
          5. The Client
      6. HTML User Interface (UI)
        1. The Client Class Passing HTML Data
        2. From a Variable Name to an Object Instance
        3. Adding a Decoration
  7. IV. Behavioral Design Patterns
    1. 9. The Template Method Pattern
      1. What Is the Template Method Pattern?
      2. When to Use the Template Method
      3. Using the Template Method with Images and Captions: A Minimal Example
        1. The Abstract Class
        2. The Concrete Class
      4. The Client
      5. The Hollywood Principle
      6. Using the Template Method with Other Design Patterns
        1. The Client’s Reduced Workload
        2. The Template Method Participants
      7. The Factory Method Participants
      8. The Hook in the Template Method Design Pattern
        1. Setting Up the Hook
        2. Implementing the Hook
        3. The Client and Tripping the Hook
          1. Setting the Boolean with comparison operators
          2. The Client class
      9. The Small and Mighty Template Method
    2. 10. The State Design Pattern
      1. What Is the State Pattern?
      2. When to Use the State Pattern?
      3. The State Machine
      4. Light On, Light Off: The Minimal State Design Pattern
        1. Context Is King
          1. State instances in the Context class
          2. Calling the state methods: Context trigger methods
          3. Setting the current state
          4. The state getters
          5. The Context class summary
        2. The States
          1. OnState
          2. OffState
        3. The Client Request through the Context
      5. Adding States
        1. Changing the Interface
        2. Changing the States
          1. OffState
          2. OnState
          3. BrighterState
          4. BrightestState
        3. Updating the Context Class
        4. An Updated Client
      6. The Navigator: More Choices and Cells
        1. Setting Up a Matrix Statechart
        2. Setting Up the Interface
        3. The Context
        4. The States
          1. Cell1State
          2. Cell2State
          3. Cell3State
          4. Cell4State
          5. Cell5State
          6. Cell6State
          7. Cell7State
          8. Cell8State
          9. Cell9State
        5. The Client Picks a Path
      7. The State Pattern and PHP
  8. V. MySQL and PHP Design Patterns
    1. 11. A Universal Class for Connections and a Proxy Pattern for Security
      1. A Simple Interface and Class for MySQL
        1. The Pregnant Interface
        2. Universal MySQL Connection Class and Static Variables
        3. Easy Client
      2. The Protection Proxy for Login
        1. Setting Up Login Registration
        2. Implementing the Login Proxy
          1. The login form and the Client
          2. The Proxy at work
          3. The real subject
      3. The Proxy and Real-World Security
    2. 12. The Flexibility of the Strategy Design Pattern
      1. Encapsulating Algorithms
        1. Differentiating the Strategy from the State Design Pattern
        2. No Conditional Statements, Please
        3. A Family of Algorithms
      2. A Minimalist Strategy Pattern
        1. The Client and the Trigger Scripts
        2. The Context Class and Strategy Interface
        3. The Concrete Strategies
          1. DataEntry
          2. DisplayData
          3. SearchData
          4. UpdateData
          5. DeleteRecord
          6. Connection interface and class
      3. Expanded Strategy Pattern with Data Security and Parameterized Algorithms
        1. A Data Security Helper Class
        2. Adding a Parameter to an Algorithm Method
        3. The Survey Table
        4. Data Entry Modules
        5. The Client Calls for Help
        6. The Minor but Major Change in Context Class
        7. The Concrete Strategies
          1. DataEntry
          2. DisplayAll
          3. SearchData
          4. UpdateData
          5. DeleteRecord
      4. The Flexible Strategy Pattern
    3. 13. The Chain of Responsibility Design Pattern
      1. Passing the Buck
      2. The Chain of Responsibility in a MySQL Help Desk
        1. Building and Loading the Response Table
          1. InsertData.php
          2. UpdateData.php
        2. The Help Desk Chain of Responsibility
          1. HTML Data Entry, Client and Request Participants
          2. Handler interface and concrete handlers
      3. Automated Chain of Responsibility and Factory Method
        1. The Chain of Responsibility and Date-Driven Requests
        2. Factory Method Finishes Job
          1. The Creator and HungerFactory
          2. The product and individual countries
          3. Helpers, resources, and style
      4. Ease of Update
    4. 14. Building a Multidevice CMS with the Observer Pattern
      1. Built-In Observer Interfaces
      2. When to Use the Observer Pattern
      3. Using SPL with the Observer Pattern
        1. SplSubject
        2. SplObserver
        3. SplObjectStorage
        4. The SPL Concrete Subject
        5. The SPL Concrete Observer
        6. The SPL Client
      4. Free Range PHP and the Observer Pattern
        1. The Abstract Subject Class and ConcreteSubject Implementation
        2. Observer and Multiple Concrete Observers
          1. ConcreteObserverDT (Desktop implementation)
          2. ConcreteObserverTablet (Tablet implementation)
          3. ConcreteObserverPhone (Smartphone implementation)
        3. The Client
      5. Making a Simple CMS
        1. CMS Utilities
          1. CMS table
          2. CMS data entry and update
        2. The Multiple Device Observer
          1. Two HTML UI documents
          2. The sniffer client
          3. The Subject classes
          4. Multiple concrete observers
            1. The mobile phone observer
            2. Tablet observer
            3. Desktop view
      6. Thinking OOP
  9. Index
  10. About the Author
  11. Colophon
  12. Special Upgrade Offer
  13. Copyright