Discover effective techniques, workarounds, and expert guidance for avoiding situations where your application might break following PHP 8 migration

Key Features

  • Get the hang of all the new features introduced in PHP 8
  • Learn how to detect potential code breaks and keep your application code running smoothly in PHP 8
  • Explore an exciting new trend - asynchronous PHP programming using Swoole and Fibers

Book Description

Thanks to its ease of use, PHP is a highly popular programming language used on over 78% of all web servers connected to the Internet. PHP 8 Programming Tips, Tricks, and Best Practices will help you to get up-to-speed with PHP 8 quickly. The book is intended for any PHP developer who wants to become familiar with the cool new features available in PHP 8, and covers areas where developers might experience backward compatibility issues with their existing code after a PHP 8 update. The book thoroughly explores best practices, and highlights ways in which PHP 8 enforces these practices in a much more rigorous fashion than its earlier versions.

You'll start by exploring new PHP 8 features in the area of object-oriented programming (OOP), followed by enhancements at the procedural level. You'll then learn about potential backward compatible breaks and discover best practices for improving performance. The last chapter of the book gives you insights into PHP async, a revolutionary new way of programming, by providing detailed coverage and examples of asynchronous programming using the Swoole extension and Fibers.

By the end of this PHP book, you'll not only have mastered the new features, but you'll also know exactly what to watch out for when migrating older PHP applications to PHP 8.

What you will learn

  • Gain a comprehensive understanding of the new PHP 8 object-oriented features
  • Discover new PHP 8 procedural programming enhancements
  • Understand improvements in error handling in PHP 8
  • Identify potential backward compatibility issues
  • Avoid traps due to changes in PHP extensions
  • Find out which features have been deprecated and/or removed in PHP 8
  • Become well-versed with programming best practices enforced by PHP 8

Who this book is for

This book is for PHP developers at all levels who have experience in PHP 5 or above. If you're just getting started with PHP, you'll find the code examples useful for learning the language. Developers who have worked for a few months on one or more PHP projects will be able to apply the tips and techniques to the code at hand, while those with many years of PHP experience are sure to appreciate the concise coverage of new PHP 8 features.

Table of Contents

  1. PHP 8 Programming Tips, Tricks and Best Practices
  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. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  7. Section 1: PHP 8 Tips
  8. Chapter 1: Introducing New PHP 8 OOP Features
    1. Technical requirements
    2. Using constructor property promotion
    3. Property promotion syntax
    4. Using property promotion for code reduction
    5. Working with attributes
    6. Overview of PHP comments
    7. PHP DocBlock considerations
    8. Hidden dangers associated with the misuse of DocBlocks
    9. The Attribute class
    10. Attribute syntax
    11. Incorporating match expressions into your program code
    12. Match expression general syntax
    13. Understanding named arguments
    14. Named argument generic syntax
    15. Calling core functions using named arguments
    16. Order independence and documentation
    17. Exploring new data types
    18. Union types
    19. mixed type
    20. The effect of a mixed type on inheritance
    21. Improving code using typed properties
    22. What are typed properties?
    23. Property typing can lead to a reduction in code
    24. Summary
  9. Chapter 2: Learning about PHP 8's Functional Additions
    1. Technical requirements
    2. Working with new PHP 8 operators
    3. Using the variadics operator
    4. Using the nullsafe operator
    5. The concatenation operator has been demoted
    6. Using nested ternary operators
    7. Using arrow functions
    8. Generic syntax
    9. Arrow functions versus anonymous functions
    10. Variable inheritance
    11. Practical example: Using arrow functions
    12. Understanding uniform variable syntax
    13. Defining uniform variable syntax
    14. How does uniform variable syntax affect PHP 8?
    15. Learning new array- and string-handling techniques
    16. Working with array_splice()
    17. Using array_slice()
    18. Detecting string beginning, middle, and end
    19. Securing SQLite databases with the authorizer
    20. Wait a minute... no security?
    21. What's a SQLite authorization callback?
    22. What gets sent to the callback?
    23. Authorizer usage example
    24. Summary
  10. Chapter 3: Taking Advantage of Error-Handling Enhancements
    1. Technical requirements
    2. Understanding PHP 8 error handling
    3. Undefined variable handling
    4. Undefined constant handling
    5. Error-level defaults
    6. Dealing with warnings that are now errors
    7. Promoted warnings in object error handling
    8. Promoted warnings in array handling
    9. Promoted warnings in string handling
    10. Understanding notices promoted to warnings
    11. Non-existent object property access handling
    12. Non-existent offset handling
    13. Misusing resource IDs as array offsets
    14. Ambiguous string offset cast
    15. Uninitialized or non-existent string offsets
    16. Handling the @ error control operator
    17. @ operator usage
    18. @ operator and error_reporting()
    19. Summary
  11. Chapter 4: Making Direct C-Language Calls
    1. Technical requirements
    2. Understanding FFI
    3. Relationship between PHP and the C language
    4. Understanding PHP extensions
    5. Learning where to use FFI
    6. Adopting FFI into PHP
    7. Do not use FFI for speed
    8. Why use the FFI extension?
    9. Examining the FFI class
    10. Working with FFI creational methods
    11. Comparing data using FFI
    12. Using FFI infrastructural methods
    13. Using FFI in an application
    14. Integrating an external C library into a PHP script
    15. Working with PHP callbacks
    16. Summary
  12. Section 2: PHP 8 Tricks
  13. Chapter 5: Discovering Potential OOP Backward-Compatibility Breaks
    1. Technical requirements
    2. Discovering core OOP coding differences
    3. Handling static calls in PHP 8
    4. Dealing with object property handling changes
    5. Working with PHP 8 autoloading
    6. Navigating changes in magic methods
    7. Dealing with constructor changes
    8. Working with changes to __toString()
    9. Taking control of serialization
    10. Understanding PHP serialization
    11. Understanding the __sleep() magic method
    12. Understanding a potential code break in the __sleep() method
    13. Learning about __wakeup()
    14. Introducing the Serializable interface
    15. Examining PHP serializable interface issues
    16. New magic methods to control PHP serialization
    17. Understanding PHP 8 expanded variance support
    18. Understanding covariant returns
    19. Using contravariant parameters
    20. Handling Standard PHP Library (SPL) changes
    21. Understanding changes to SplFileObject
    22. Examining changes to SplHeap
    23. Handling changes in SplDoublyLinkedList
    24. Summary
  14. Chapter 6: Understanding PHP 8 Functional Differences
    1. Technical requirements
    2. Learning key advanced string handling differences
    3. Handling changes to the needle argument
    4. Dealing with v*printf() changes
    5. Working with null length arguments in PHP 8
    6. Examining changes to implode()
    7. Learning about constants usage in PHP 8
    8. Understanding PHP 8 string-to-numeric comparison improvements
    9. Learning about strict and non-strict comparisons
    10. Examining numeric strings
    11. Detecting backward-compatible breaks involving numeric strings
    12. Dealing with inconsistent string-to-numeric comparison results
    13. Understanding comparison changes made in PHP 8
    14. Avoiding problems during a PHP 8 upgrade
    15. Handling differences in arithmetic, bitwise, and concatenation operations
    16. Handling non-scalar data types in arithmetic and bitwise operations
    17. Examining changes in the order of precedence
    18. Taking advantage of locale independence
    19. Understanding the problems associated with locale dependence
    20. Reviewing functions and operations affected by locale independence
    21. Handling arrays in PHP 8
    22. Dealing with negative offsets
    23. Handling curly brace usage changes
    24. Mastering changes in security functions and settings
    25. Understanding changes in disabled functions handling
    26. Learning about changes to the crypt() function
    27. Dealing with changes to password_hash()
    28. Learning about changes to assert()
    29. Summary
  15. Chapter 7: Avoiding Traps When Using PHP 8 Extensions
    1. Technical requirements
    2. Understanding the shift from resources to objects
    3. PHP 8 extension resource-to-object migration
    4. Potential code break involving is_resource()
    5. Advantages of objects over resources
    6. Traversable to IteratorAggregate migration
    7. Learning about changes to XML extensions
    8. Examining XMLWriter extension differences
    9. Working with changes to the SimpleXML extension
    10. Understanding other XML extension changes
    11. Avoiding problems with the updated mbstring extension
    12. Discovering needle-argument differences in mb_str*() functions
    13. Examining changes to mb_ereg*() functions
    14. Dealing with changes to the GD extension
    15. GD extension resource-to-object migration
    16. GD extension compile flag changes
    17. Other GD extension changes
    18. Discovering changes to the Reflection extension
    19. Reflection extension usage
    20. Learning about Reflection extension improvements
    21. Working with other extension gotchas
    22. New database extension operating system library requirements
    23. Reviewing changes to the ZIP extension
    24. Examining PCRE extension changes
    25. Working with Intl extension changes
    26. Understanding cURL extension changes
    27. Reviewing COM extension changes
    28. Examining other extension changes
    29. Summary
  16. Chapter 8: Learning about PHP 8's Deprecated or Removed Functionality
    1. Technical requirements
    2. Discovering what has been removed from the core
    3. Examining functions removed in PHP 8
    4. Discovering other PHP 8 usage changes
    5. Examining core deprecations
    6. Deprecated usage in parameter order
    7. Working with removed functionality in PHP 8 extensions
    8. Discovering mbstring extension changes
    9. Reworking code that uses Reflection*::export()
    10. Discovering other deprecated PHP 8 extension functionality
    11. Changes to the XML-RPC extension
    12. Changes made to the DOM extension
    13. Dealing with deprecated or removed security-related functionality
    14. Examining PHP 8 stream-filter changes
    15. Dealing with custom error-handling changes
    16. Dealing with changes to backtraces
    17. PDO error-handling mode default changed
    18. Examining the track_errors php.ini setting
    19. Summary
  17. Section 3: PHP 8 Best Practices
  18. Chapter 9: Mastering PHP 8 Best Practices
    1. Technical requirements
    2. Discovering method signature changes
    3. Managing magic method signatures
    4. Examining Reflection method signature changes
    5. Dealing with PDO extension signature changes
    6. Dealing with newly defined static methods
    7. Working with the static return type
    8. Extending the use of the ::class constant
    9. Taking advantage of trailing commas
    10. Learning about methods that are no longer required
    11. Working with interfaces and traits
    12. Discovering new DOM extension interfaces
    13. Using new DateTime methods
    14. Understanding PHP 8 trait handling refinements
    15. Dealing with private methods
    16. Controlling anonymous class usage
    17. Understanding changes in namespaces
    18. Discovering differences in tokenization
    19. Using reserved keywords in a namespace
    20. Exposing bad namespace naming practices
    21. Summary
  19. Chapter 10: Improving Performance
    1. Technical requirements
    2. Working with the JIT compiler
    3. Discovering how PHP works without JIT
    4. Enabling the JIT compiler
    5. Configuring the tracing mode
    6. Using the JIT compiler
    7. Debugging with the JIT compiler
    8. Discovering additional JIT compiler settings
    9. Speeding up array handling
    10. Working with SplFixedArray in PHP 8
    11. Implementing stable sort
    12. Understanding stable sorts
    13. Contrasting stable and non-stable sorting
    14. Examining the effect of stable sorting on keys
    15. Handling illegal sort functions
    16. Using weak references to improve efficiency
    17. Taking advantage of weak references
    18. Reviewing the WeakReference class definition
    19. Using weak references
    20. Working with WeakMap
    21. Implementing a container class using SplObjectStorage
    22. Understanding the benefits of WeakMap over SplObjectStorage
    23. Summary
  20. Chapter 11: Migrating Existing PHP Apps to PHP 8
    1. Technical requirements
    2. Understanding development, staging, and production environments
    3. Defining an environment
    4. Learning how to spot BC breaks before a migration
    5. Gaining an overview of BC breaks
    6. Creating a BC break scan configuration file
    7. Developing a BC break scan class
    8. Performing the migration
    9. Step 1 – Review the migration guide
    10. Step 2 – Make sure the current code works
    11. Step 3 – Back up everything
    12. Step 4 – Create a version control branch
    13. Step 5 – Scan for BC breaks
    14. Step 6 – Fix incompatibilities
    15. Step 7 – Repeat steps 5 and 6 as needed
    16. Step 8 – Commit changes to the repository
    17. Step 9 – Test in a simulated virtual environment
    18. Step 10 – Return to step 5 if the test is unsuccessful
    19. Step 11 – Install PHP 8 on the staging environment
    20. Step 12 – Test and clone the staging environment to production
    21. Testing and troubleshooting the migration
    22. Testing and troubleshooting tools
    23. Handling issues with Composer
    24. Working with unit tests
    25. Summary
  21. Chapter 12: Creating PHP 8 Applications Using Asynchronous Programming
    1. Technical requirements
    2. Understanding the PHP async programming model
    3. Developing synchronous programming code
    4. Understanding the asynchronous programming model
    5. Working with async coroutine support
    6. Creating a PHP async application
    7. Using the Swoole extension
    8. Examining the Swoole extension
    9. Installing the Swoole extension
    10. Testing the installation
    11. Examining a sample I/O-intensive application
    12. Using the Swoole extension to improve application performance
    13. Using selected PHP frameworks in async mode
    14. Working with ReactPHP
    15. Implementing PHP async using Amp
    16. Using Mezzio with Swoole
    17. Working with the parallel extension
    18. Learning about PHP 8.1 fibers
    19. Discovering the Fiber class
    20. Using fibers
    21. Examining the effect of fibers on ReactPHP and Swoole
    22. Summary
    23. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts