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:
- 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:
- Install Docker Compose. For all operating systems, start here:
- 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. - 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
- 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 thedocker-compose build
command to build the environment. Note that you might needroot
(administrator) privileges to run Docker commands. If this is the case, either run as administrator (for Windows) or preface the command withsudo
. Depending on your connection speed, the initial build might take quite a bit of time to complete! - To bring the container up, proceed as follows
- 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 casedocker build
will suffice.Here's a useful command to ensure all containers are running:
docker-compose ps
- 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
- 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
- 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.