All Projects → PacktPublishing → PHP-8-Programming-Tips-Tricks-and-Best-Practices

PacktPublishing / PHP-8-Programming-Tips-Tricks-and-Best-Practices

Licence: MIT License
PHP 8 Programming Tips, Tricks and Best Practices, published by Packt

Programming Languages

PHP
23972 projects - #3 most used programming language
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects
python
139335 projects - #7 most used programming language
Makefile
30231 projects

PHP 8 Programming Tips, Tricks and Best Practices

PHP 8 Programming Tips, Tricks and Best Practices

This is the code repository for PHP 8 Programming Tips, Tricks and Best Practices, published by Packt.

A practical guide to PHP 8 features, usage changes, and advanced programming techniques

What is this book about?

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.

This book covers the following exciting features:

  • Gain a comprehensive understanding of the new PHP 8 object-oriented features
  • Discover new PHP 8 procedural programming enhancements
  • Understand improvements in error handling in PHP 8
  • Identify potential backward compatibility issues
  • Avoid traps due to changes in PHP extensions
  • Find out which features have been deprecated and/or removed in PHP 8
  • Become well-versed with programming best practices enforced by PHP 8

If you feel this book is for you, get your copy today!

https://www.packtpub.com/

Instructions and Navigations

All of the code is organized into folders.

The code will look like the following:

// /repo/ch01/php7_prop_reduce.php 

declare(strict_types=1); 

class Test { 

 protected $id = 0; 

 protected $token = 0; 

 protected $name = '';o

Following is what you need for this book: This book is for PHP developers at all levels who have experience in PHP 5 or above. If you're just getting started with PHP, you'll find the code examples useful for learning the language. Developers who have worked for a few months on one or more PHP projects will be able to apply the tips and techniques to the code at hand, while those with many years of PHP experience are sure to appreciate the concise coverage of new PHP 8 features.

With the following software and hardware list you can run all code files present in the book (Chapter 1-13).

Software and Hardware List

Chapter Software required OS required
1-13 PHP 8.x , Key PHP extensions , Docker and docker-compose Windows, Mac OS X, and Linux (Any)

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.

Related products

Get to Know the Author

Doug Bierer has been hooked on computers since his first program, written on a DEC PDP-8, in 1971. In his wide-ranging career, he has been a professional contract programmer since 1978, having written applications in BASIC, PL/I, assembler, FORTH, C, C++, dBase/FoxBase/Clipper, Pascal, Perl, Java, and PHP. He deployed his first website in 1993 while living in San Francisco. He speaks four languages, has traveled extensively, and now resides in Thailand. He also spent some years doing system administration and TCP/IP networking. Some of his technical works include PHP 7 Programming Cookbook and Learning MongoDB 4.x (Packt), as well as Learning PHP and MySQL, Learning PHP Security and Learning Doctrine (O'Reilly Media).

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

  1. Install Docker
  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 /path/to/repo
* Otherwise, you can simply download and unzip from this URL: [https://github.com/PacktPublishing/PHP-8-Programming-Tips-Tricks-and-Best-Practices/archive/main.zip](https://github.com/PacktPublishing/PHP-8-Programming-Tips-Tricks-and-Best-Practices/archive/main.zip)
* And then unzip into a folder you create which we refer to as `/path/to/repo` in this book.

Windows Instructions

Please note that many of the command-line options mentioned here can also be accomplished from Docker Desktop for Windows

Install the Docker containers associated with the book

You must first build the two docker containers associated with this book online, one that runs PHP 7.1, the other runs PHP 8.x. Please note that the initial build might take up to 15 minutes to complete!

  1. From your local computer, open a command prompt.
  2. Change directory to /path/to/repo.
cd C:\path\to\repo
  1. First time only, issue this command to build the environment:
init build

Bring the containers online

Once the container has been built, proceed as follows to bring the containers online:

  1. From your local computer, open a command prompt.
  2. Change directory to /path/to/repo.
cd C:\path\to\repo
  1. Bring the docker container online in background mode:
init up

Container access

Browser access

To access the docker containers from your browser, first make sure the containers are online (see previous).

  1. Open the browser on your local computer.
  2. Enter this URL to access the PHP 7 container: http://localhost:7777
    • Alternate URL: http://172.16.0.77/
  3. Enter this URL to access the PHP 8 container: http://localhost:8888
    • Alternate URL: http://172.16.0.88/

Command line access

To access the containers from a command shell proceed as follows:

  1. From your local computer, open a command prompt.
  2. Change to /path/to/repo (use the appropriate drive letter if not drive C):
cd C:\path\to\repo
  1. Execute the command to open a command shell:
  • To open a shell into the PHP 7 container:
init shell 7
  • To open a shell into the PHP 8 container:
init shell 8

Bring container offline

It's a good idea to bring the container down (offline) when you're finished working with it. This conserves resources. Proceed as follows:

  1. From your local computer, open a command prompt (terminal window).
  2. Change to /path/to/repo (use the appropriate drive letter if not drive C):
cd C:\path\to\repo
  1. Use init.bat to bring the containers offline:
init down

Please note that after the containers have been shut down, the ownership of all files in /path/to/repo are reset to the current user using the Windows takeown command.

Linux and Mac Instructions

Please note that many of the command-line options mentioned here can also be accomplished from Docker Desktop for Mac (Mac only!).

Install the Docker containers associated with the book

You must first build the two docker containers associated with this book online, one that runs PHP 7.1, the other runs PHP 8.x. Please note that the initial build might take up to 10 minutes to complete!

  1. From your local computer, open a terminal window.
  2. Change directory to /path/to/repo.
cd /path/to/repo
  1. First time only, issue this command to build the environment:
./init.sh build

Bring the containers online

Once the container has been built, proceed as follows to bring the containers online:

  1. From your local computer, open a terminal window.
  2. Change directory to /path/to/repo.
cd /path/to/repo
  1. Bring the docker container online in background mode:
./init.sh up

Container access

Browser access

To access the docker containers from your browser, first make sure the containers are online (see previous).

  1. Open the browser on your local computer.
  2. Enter this URL to access the PHP 7 container: http://localhost:7777
    • Alternate URL: http://172.16.0.77/
  3. Enter this URL to access the PHP 8 container: http://localhost:8888
    • Alternate URL: http://172.16.0.88/ If the web page doesn't come up right away you might need to restart the container web server:
  4. From your local computer, open a terminal window.
  5. Change to /path/to/repo:
  6. Use the init option to initialize the web server:
  • Windows
init init
  • Linux/Mac
./init.sh init

Command line access

To access the containers from a command shell proceed as follows:

  1. From your local computer, open a terminal window.
  2. Change to /path/to/repo:
cd /path/to/repo
  1. Execute the command to open a command shell:
  • To open a shell into the PHP 7 container:
./init.sh shell 7
  • To open a shell into the PHP 8 container:
./init.sh shell 8

Bring container offline

It's a good idea to bring the container down (offline) when you're finished working with it. This conserves resources. Proceed as follows:

  1. From your local computer, open a terminal window (terminal window).
  2. Change to /path/to/repo (use the appropriate drive letter if not drive C):
cd /path/to/repo
  1. Use init.bat to bring the containers offline:
./init.sh down

Please note that after the containers have been shut down, the ownership of all files in /path/to/repo are reset to the current user.

Troubleshooting

One reader ran into this error:

standard_init_linux.go:228: exec user process caused: no such file or directory
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].