The Art of Modern PHP 8

Book description

Discover how to apply the principles of OOP, types, and package management the right way

Key Features

  • Explore the latest PHP 8.1 features and modern web development practices
  • Refactor your legacy codebase using modern features and styles in PHP
  • Create your own Composer packages that can be reused across all PHP projects

Book Description

PHP has come a long way since its introduction. While the language has evolved with PHP 8, there are still a lot of websites running on a version of PHP that is no longer supported. If you are a PHP developer working with legacy PHP systems and want to discover the tenants of modern PHP, this is the book for you.

The Art of Modern PHP 8 walks you through the latest PHP features and language concepts. The book helps you upgrade your knowledge of PHP programming and practices. Starting with object-oriented programming (OOP) in PHP and related language features, you'll work through modern programming techniques such as inheritance, understand how it contrasts with composition, and finally look at more advanced language features. You'll learn about the MVC pattern by developing your own MVC system and advance to understanding what a DI container does by building a toy DI container. The book gives you an overview of Composer and how to use it to create reusable PHP packages. You’ll also find techniques for deploying these packages to package libraries for other developers to explore.

By the end of this PHP book, you'll have equipped yourself with modern server-side programming techniques using the latest versions of PHP.

What you will learn

  • Understand how to use modern PHP features such as objects, types, and more
  • Get to grips with PHP package management using Composer
  • Explore dependency injection for your PHP applications
  • Find out what the differences are between legacy and modern PHP 8 code and practices
  • Write clean PHP code and implement design patterns
  • Get hands-on with modern PHP using examples applying MVC and DI techniques

Who this book is for

The book is for existing PHP developers and CTO-level professionals who are working with PHP technologies, including legacy PHP, in production. The book assumes beginner-level knowledge of PHP programming as well as experience with server-side development.

Table of contents

  1. The Art of Modern PHP 8
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
  7. Section 1 – PHP 8 OOP
  8. Chapter 1: Object-Oriented PHP
    1. What is OOP?
      1. Understanding the phrase
      2. OOP alternatives
      3. Learning object-oriented PHP
    2. PHP OOP basics
      1. Classes
      2. Interfaces
      3. Namespaces
    3. Summary
  9. Chapter 2: Inheritance and Composition, Encapsulation and Visibility, Interfaces and Concretions
    1. Encapsulation and visibility
      1. Private, protected, and public
    2. Inheritance–the classical parent, child, and "is a" style
      1. Constructor promotion
    3. Composition–the modern, flexible "has a" style
      1. Interfaces instead of abstract classes
    4. Summary
  10. Chapter 3: Advanced OOP Features
    1. SPL – the Standard PHP Library
      1. Data structures
      2. Iterators
    2. Exceptions and error handling
      1. Yoda and Pokemon exception handling
      2. Exception and error handling best practices
    3. Meta-programming with reflection and attributes
      1. Reflection
      2. Attributes
    4. Summary
  11. Section 2 – PHP Types
  12. Chapter 4: Scalar, Arrays, and Special Types
    1. Scalar types
      1. Strings
      2. Ints
      3. Floats
      4. Bools
      5. Type juggling
      6. Type and value comparison
    2. Arrays and iterables
      1. Arrays
      2. Iterables
      3. DocBlock types
    3. Nullable, void, and uninitialized
      1. Null and nullable
      2. Void
      3. Uninitialized
    4. Resources
    5. Summary
  13. Chapter 5: Object Types, Interfaces, and Unions
    1. Object type inheritance
      1. Object comparison
      2. Passing objects by reference
      3. Making new objects
    2. Summary
  14. Chapter 6: Parameter, Property, and Return Types
    1. Coercive and strict modes
    2. Covariance and contravariance
      1. Contravariance – that is, allowing less specific parameter types than the parent
      2. Covariance – that is, allowing more specific return types than the parent
      3. Remembering the rules
    3. Summary
  15. Section 3 – Clean PHP 8 Patterns and Style
  16. Chapter 7: Design Patterns and Clean Code
    1. Clean Code Acronyms (CCA)
      1. DRY
      2. KICK
    2. Design patterns
      1. Creational patterns
      2. Structural patterns
      3. Behavioral patterns
      4. Architectural patterns
    3. Summary
  17. Chapter 8: Model, View, Controller (MVC) Example
    1. Model, View, Controller – MVC
    2. Controller
      1. Front controller
    3. Model
      1. Entity pattern
      2. Universally unique identifier (UUID)
      3. Persistence and ORM
      4. Singleton anti-pattern
      5. Repository pattern
      6. Collection pattern
    4. View
    5. It lives
    6. Summary
  18. Chapter 9: Dependency Injection Example
    1. Inversion of control container
    2. Dependency injection and the service locator
    3. The service locator anti-pattern
    4. Summary
  19. Section 4 – PHP 8 Composer Package Management (and PHP 8.1)
  20. Chapter 10: Composer For Dependencies
    1. What is Composer?
      1. What are dependencies?
      2. Dependency resolution
      3. But wait, there's more
      4. In a nutshell, it's awesome
      5. Get it installed!
    2. Composer autoloader
      1. Old-fashioned versus autoloaded
      2. A real-world, old-fashioned example
      3. Extra points
      4. Autoloading in PHP
      5. Configuring Composer autoloader
    3. Using Composer to require packages
      1. Finding dependencies
      2. Initialization of composer.json
      3. What is a vendor?
      4. Minimum stability
      5. Package type
      6. License
      7. So far, so good
      8. Installing monolog
      9. Ignore the vendor directory in your VCS
      10. The composer.lock file
      11. Adding extra dependencies
    4. Version constraints
      1. SemVer in a nutshell
      2. Caret (^) Version Range
      3. Tilde (~) Version Range
      4. Exact version
      5. Version range
      6. Multiple acceptable versions
      7. Full documentation
    5. Dev and prod dependencies
      1. Requiring dev dependencies
      2. Installing without dev dependencies
    6. Summary
  21. Chapter 11: Creating Your Own Composer Package
    1. Writing composer.json
      1. Name
      2. Description
      3. Keywords
      4. License
    2. Using source folders and namespaces
    3. Executable Binaries Configured under bin
      1. Extensionless files
      2. Executable permissions
      3. Finding an autoloader
    4. Using versions and tagging
      1. Creating a Git tag
      2. SemVer
      3. Stable and development versions
    5. Private Git repos as Composer dependencies
    6. Publishing on Packagist
      1. Things to check before you publish
      2. Submitting your package to Packagist
      3. Created
      4. If you were just testing, delete it
    7. Summary
  22. Section 5 – Bonus Section - PHP 8.1
  23. Chapter 12: The Awesomeness That Is 8.1
    1. Enumerations
      1. Basic and backed enums
      2. Creating enum instances
      3. Not just classes with cases
    2. Readonly properties
      1. Readonly gotchas
    3. Intersection types
    4. Never return type
    5. Final constants
    6. Summary – end of the book
    7. Why subscribe?
  24. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: The Art of Modern PHP 8
  • Author(s): Joseph Edmonds
  • Release date: October 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800566156