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)
1
Section 1: PHP 8 Tips
6
Section 2: PHP 8 Tricks
11
Section 3: PHP 8 Best Practices

Technical requirements

To examine and run the code examples provided in this chapter, the minimum recommended hardware is listed here:

  • x86_64-based desktop PC or laptop
  • 1 gigabyte (GB) free disk space
  • 4 GB of random-access memory (RAM)
  • 500 kilobits per second (Kbps) or faster internet connection

In addition, you will need to install the following software:

  • Docker
  • Docker Compose

This book uses a pre-built Docker image that contains all the needed software to create and run the PHP 8 code examples covered in this book. You do not need to install PHP, Apache, or MySQL on your computer: just use Docker and the provided image.

To set up a test environment to run the code examples, proceed as follows:

  1. Install Docker.

    If you are running Windows, start here:

    https://docs.docker.com/docker-for-windows/install/

    If you are on a Mac, start here:

    https://docs.docker.com/docker-for-mac/install/

    If you are on Linux, have a look here:

    https://docs.docker.com/engine/install/

  2. Install Docker Compose. For all operating systems, start here:

    https://docs.docker.com/compose/install/

  3. Install the source code associated with this book onto your local computer.

    If you have installed Git, use the following command:

    git clone https://github.com/PacktPublishing/PHP-8-Programming-Tips-Tricks-and-Best-Practices.git ~/repo

    Otherwise, you can simply download the source code from this Uniform Resource Locator (URL): https://github.com/PacktPublishing/PHP-8-Programming-Tips-Tricks-and-Best-Practices/archive/main.zip. You can then unzip into a folder you create, which we refer to as /repo in this book.

  4. You can now start the Docker daemon running. For Windows or Mac, all you need to do is to activate the Docker Desktop app.

    If you are running Ubuntu or Debian Linux, issue this command:

    sudo service docker start

    For Red Hat, Fedora, or CentOS, use this command:

    sudo systemctl start docker

  5. Build a Docker container associated with this book and bring it online. To do so, proceed as follows.

    From your local computer, open Command Prompt (terminal window). Change the directory to /repo. For the first time only, issue the docker-compose build command to build the environment. Note that you might need root (administrator) privileges to run Docker commands. If this is the case, either run as administrator (for Windows) or preface the command with sudo. Depending on your connection speed, the initial build might take quite a bit of time to complete!

  6. To bring the container up, proceed as follows
  7. From your local computer, open Command Prompt (terminal window). Change the directory to /repo. Bring the Docker container online in background mode by running the following command:
    docker-compose up -d

    Note that you actually don't need to build the container separately. If the container is not built when you issue the docker-compose up command, it will be built automatically. On the other hand, it might be convenient to build the container separately, in which case docker build will suffice.

    Here's a useful command to ensure all containers are running:

    docker-compose ps
  8. To access the running Docker container web server, proceed as follows.

    Open the browser on your local computer. Enter this URL to access PHP 8 code:

    http://localhost:8888

    Enter this URL to access PHP 7 code:

    http://localhost:7777

  9. To open a command shell into the running Docker container, proceed as follows.

    From your local computer, open Command Prompt (terminal window). Issue this command to access the PHP 8 container:

    docker exec -it php8_tips_php8 /bin/bash 

    Issue this command to access the PHP 7 container:

    docker exec -it php8_tips_php7 /bin/bash
  10. When you are finished working with the container, to take it offline open Command Prompt (terminal window) from your local computer and issue this command:
    docker-compose down 

The source code for this chapter is located here:

https://github.com/PacktPublishing/PHP-8-Programming-Tips-Tricks-and-Best-Practices

Important note

If your host computer uses Advanced RISC Machines (ARM) architecture (for example, Raspberry Pi), you will need to use a modified Dockerfile.

Tip

It would be an excellent idea to get a quick overview of Docker technology and terms by reviewing this article: https://docs.docker.com/get-started/.

We can now begin our discussion by having a look at constructor property promotion.