-
Book Overview & Buying
-
Table Of Contents
PHP 8 Programming Tips, Tricks and Best Practices
By :
To examine and run the code examples provided in this chapter, the minimum recommended hardware is listed here:
In addition, you will need to install the following software:
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:
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:
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.
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
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!
/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
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
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
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.