Book Image

PHP 8 Programming Tips, Tricks and Best Practices

By : Doug Bierer
Book Image

PHP 8 Programming Tips, Tricks and Best Practices

By: Doug Bierer

Overview of this book

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.
Table of Contents (17 chapters)
Section 1: PHP 8 Tips
Section 2: PHP 8 Tricks
Section 3: PHP 8 Best Practices

What this book covers

Chapter 1, Introducing New PHP 8 OOP Features, introduces you to new PHP 8 features specific to Object-Oriented Programming (OOP). The chapter features plenty of short code examples that clearly illustrate the new features and concepts. This chapter is critical in helping you quickly take advantage of the power of PHP 8 and adapt the code examples to your own practice.

Chapter 2, Learning about PHP 8's Functional Additions, covers important additions and enhancements introduced to PHP 8 at the procedural level. It includes plenty of code examples that show new PHP 8 features and techniques to facilitate procedural programming. This chapter teaches you how to write faster and cleaner application code.

Chapter 3, Taking Advantage of Error-Handling Enhancements, explores one of the key improvements in PHP 8, its advanced error handling capabilities. In this chapter, you learn which Notices have been upgraded to Warnings, as well as which Warnings have now been promoted to Errors. This chapter will give you an excellent understanding of the background and intent of security enhancements allowing you to better control the use of your code. In addition, it's critical to be aware of error conditions that formerly only generated Warnings, but now generate Errors, so that you take measures to prevent your applications from failing following an upgrade to PHP 8.

Chapter 4, Making Direct C-Language Calls, helps you to learn what the Foreign Function Interface (FFI) is all about, what it's good for, and how to use it. The information in this chapter is important for developers interested in rapid custom prototyping using direct C language calls. This chapter shows you how to incorporate C language structures and functions directly into your code, opening the doors to an entire world of functionality hitherto unavailable to PHP.

Chapter 5, Discovering Potential OOP Backward-Compatibility Breaks, introduces you to new PHP 8 features specific to OOP. The chapter features plenty of short code examples that clearly illustrate the new features and concepts. This chapter is critical in helping you quickly take advantage of the power of PHP 8 by adapting the code examples into your own practice. In addition, this chapter highlights situations where object-oriented code might break after a PHP 8 migration.

Chapter 6, Understanding PHP 8 Functional Differences, covers potential backwards d-compatibility breaks at the PHP 8 command or functional level. This chapter presents important information that highlights potential pitfalls when migrating existing code to PHP 8. The information presented in this chapter enables you to produce reliable PHP code. After working through the concepts in this chapter, you'll be in a better position to write code that produces precise results and avoids inconsistencies.

Chapter 7, Avoiding Traps When Using PHP 8 Extensions, takes you through the major changes to extensions that have been made and how to avoid traps when updating an existing application to PHP 8. Once you finish reviewing the sample code and topics presented, you'll be able to prepare any existing PHP code for migration to PHP 8. In addition to learning about the changes to the various extensions, you'll also gain deep insight into their operation. This will allow you to make informed decisions when using extensions in PHP 8.

Chapter 8, Learning about PHP 8's Deprecated or Removed Functionality, walks you through functionality that has been deprecated or removed in PHP 8. After you have read the material in this chapter and followed the example application code, you will be able to detect and rewrite code that has been deprecated. You will also learn how to develop workarounds for functionality that has been removed as well as how to refactor code that uses removed functionality involving extensions. Another important skill you will learn in this chapter is how to improve application security by rewriting code depending on functions that have been entirely removed in PHP 8.

Chapter 9, Mastering PHP 8 Best Practices, introduces you to best practices now enforced in PHP 8. It covers a number of significant method signature changes and how their new usage continues the general PHP trend of helping you to produce better code. You will also learn about changes in the use of private methods, interfaces, traits, and anonymous classes, and how namespaces are now parsed. Mastering the best practices covered in this chapter will not only move you toward writing better code but will help you avoid the potential code breaks that might arise if you fail to grasp these new practices.

Chapter 10, Improving Performance, introduces you to a number of new PHP 8 features that have a positive effect on performance, with a special focus on the new Just-In-Time compiler. This chapter also includes thorough coverage of weak references whose proper use results in applications that use far less memory. By carefully reviewing the material covered in this chapter and by studying the code examples, you will be able to write faster and more efficient code.

Chapter 11, Migrating Existing PHP Apps to PHP 8, introduces a set of classes that form the basis of a PHP 8 backward-compatible break scanner. Throughout the book, you are shown potential code breaks that might follow a PHP 8 update. In addition, you will learn about the recommended process for migrating an existing customer PHP application to PHP 8. This chapter will leave you much better equipped to handle a PHP 8 migration, giving you the ability to perform PHP 8 migrations with greater confidence and a minimum number of problems.

Chapter 12, Creating PHP 8 Applications Using Asynchronous Programming, explains the difference between the traditional synchronous and asynchronous programming models. In recent years, an exciting new technology has taken the PHP community by storm: asynchronous programming, also known as PHP async. In addition, popular PHP async extensions and frameworks, including the Swoole extension and ReactPHP are covered, with plenty of examples to get you started. By the time you are done working through this chapter, you will be in a position to improve the performance of your applications, making them anywhere from 5 times up to a staggering 40 times faster!