O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering PHP Design Patterns

Book Description

Develop robust and reusable code using a multitude of design patterns for PHP 7

About This Book

  • Learn about advanced design patterns in PHP 7
  • Understand enhanced architectural patterns
  • Learn to implement reusable design patterns to address common recurring problems

Who This Book Is For

This book is for PHP developers who wish to have better organization structure over their code through learning common methodologies to solve architectural problems against a backdrop of learning new functionality in PHP 7.

What You Will Learn

  • Recognize recurring problems in your code with Anti-Patterns
  • Uncover object creation mechanisms using Creational Patterns
  • Use Structural design patterns to easily access your code
  • Address common issues encountered when linking objects using the splObserver classes in PHP 7
  • Achieve a common style of coding with Architectural Patterns
  • Write reusable code for common MVC frameworks such as Zend, Laravel, and Symfony
  • Get to know the best practices associated with design patterns when used with PHP 7

In Detail

Design patterns are a clever way to solve common architectural issues that arise during software development. With an increase in demand for enhanced programming techniques and the versatile nature of PHP, a deep understanding of PHP design patterns is critical to achieve efficiency while coding.

This comprehensive guide will show you how to achieve better organization structure over your code through learning common methodologies to solve architectural problems. You’ll also learn about the new functionalities that PHP 7 has to offer.

Starting with a brief introduction to design patterns, you quickly dive deep into the three main architectural patterns: Creational, Behavioral, and Structural popularly known as the Gang of Four patterns. Over the course of the book, you will get a deep understanding of object creation mechanisms, advanced techniques that address issues concerned with linking objects together, and improved methods to access your code.

You will also learn about Anti-Patterns and the best methodologies to adopt when building a PHP 7 application. With a concluding chapter on best practices, this book is a complete guide that will equip you to utilize design patterns in PHP 7 to achieve maximum productivity, ensuring an enhanced software development experience.

Style and approach

The book covers advanced design patterns in detail in PHP 7 with the help of rich code-based examples.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Mastering PHP Design Patterns
    1. Mastering PHP Design Patterns
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Why "Good PHP Developer" Isnt an Oxymoron
      1. Coding style - the PSR standards
      2. Revising object-oriented programming
        1. Polymorphism
        2. Traits (multiple inheritance)
        3. Scalar type hints
        4. Limiting debug access to private/protected properties
      3. Setting up the environment with Composer
      4. The Gang of Four (GoF)
        1. Creational design patterns
          1. Dependency injection
        2. Structural design patterns
        3. Behavioral design patterns
        4. Architectural patterns
      5. Summary
    8. 2. Anti-Patterns
      1. Why anti-patterns matter
      2. Not invented here syndrome
        1. Third-party dependencies with Composer
      3. God objects
      4. Environment variables in PHP source
      5. Singletons (and why you should be using dependency injection)
        1. Dependency injection
      6. Database as IPC
      7. Auto-increment database IDs
      8. Cronjob imitating service
      9. Software in place of architecture
      10. Interface Bloat
      11. Cart before the horse
      12. Separation of development and operations
      13. Excessive separation of development responsibilities
      14. Error suppression operator
      15. Blind faith
      16. Sequential coupling
      17. The big rewrite
        1. Automated tests
        2. Service splitting
        3. Perfectly staged migrations
      18. Tester-Driven Development
      19. Bloated optimization
        1. Analysis paralysis
        2. Bikeshedding
        3. Premature optimization
      20. Uneducated manager syndrome
      21. Wrong rocky foundations
      22. Long methods
      23. Magic numbers
      24. Summary
    9. 3. Creational Design Patterns
      1. Software design process
      2. Simple Factory
      3. Factory Method
      4. Abstract Factory pattern
      5. Lazy initialization
      6. Builder pattern
      7. Prototype pattern
      8. Summary
    10. 4. Structural Design Patterns
      1. Agile software architecture
      2. Decorator
      3. Adapter
        1. Class Adapter
        2. Object Adapter
      4. FlyWeight
      5. Composite
      6. Bridge
      7. Proxy pattern
      8. Facade
      9. Summary
    11. 5. Behavioral Design Patterns
      1. Personality traits for passionate programmers
      2. Observer pattern (SplObserver/SplSubject)
      3. Iterators
        1. IteratorAggregate
        2. Iterator
        3. The many iterators of PHP
      4. Generators
      5. Template Method design pattern
      6. Chain of Responsibility
      7. Strategy design pattern
      8. Specification design pattern
      9. Scheduled Task pattern
      10. Summary
    12. 6. Architectural Patterns
      1. Model-View-Controller (MVC)
      2. Service-oriented architecture
      3. Microservices
      4. Asynchronous queueing
        1. Message Queue pattern (Getting started with RabbitMQ)
        2. Publish-Subscriber pattern
      5. Summary
    13. 7. Refactoring
      1. What is refactoring?
      2. Test, test, and test again
      3. Code smells
        1. Long methods and duplicated code
        2. Large class
        3. Replacing complex logical statements and switch statements with polymorphism or the Strategy Pattern
        4. Duplicating code following a single control structure
        5. Long Parameter List and primitive obsession
        6. Indecent exposure
        7. Feature envy
        8. Inappropriate intimacy
        9. Deeply nested statements
        10. Remove assignments to parameters
        11. Comments
        12. Encapsulating Composite with Builder
        13. Replacing hard-coded notifications with Observer
        14. Replacing one/many distinctions with Composite
        15. Separate versions with Adapters
      4. What do I tell my manager?
      5. Summary
    14. 8. How to Write Better Code
      1. Nature of a HTTP request
      2. RESTful API design
        1. Stateless nature
        2. Versioning
        3. Filtering
        4. Sorting
        5. Searching
        6. Limiting fields
        7. Returning new fields
      3. When in doubt – KISS
      4. Software development life cycle
      5. On Scrum, and real Agility
      6. You need to sack people sometimes
      7. Lean project management
      8. YAGNI and defering decisions
      9. Monitoring
      10. Tests fight legacy
      11. Behavior-Driven Development
      12. Summary