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

Using FFI in an application

Any shared C library (generally with a *.so extension) can be included in a PHP application using the FFI extension. If you plan to work with any of the core PHP libraries or libraries produced when PHP extensions are installed, it's important to note that you have the ability to modify the behavior of the PHP language itself.

Before we examine how that works, let's first have a look at incorporating an external C library into a PHP script using the FFI extension.

Integrating an external C library into a PHP script

For the purposes of illustration, we use a simple function that might have originated from a Computer Science 101 (CS101) class: the famous bubble sort. This algorithm is widely used in beginner's computer science classes because it's easy to follow.

Important note

The bubble sort is an extremely inefficient sort algorithm and has long been superseded by faster sorting algorithms such as the shell sort, quick...