research-documentation 05-php8
PHP 8
Constructor Property Promotion
A newer feature of PHP 8 was the reduction in the amount of code needed to initialize a constructor method in a class. Previously, it was required to initialize the variables outside of the constructor, assign them to default values in the constructor, then use $this-> to assign each value in the constructor to the property outside of the constructor. Now, all of that can be completed in the constructor, with no code needed outside of it in the class.
Nullsafe Operators
In the past, checking for nulls required the use of a conditional statement using logical operators to test if a variable is null [ if($session == null) {do something...}]. Now, a question mark can be used to test for null, and they can even be chained together to create a seamless chain of null checks. If anything in the chain tests as null, the chain will stop being evaluated, and test as null [$item = $price?->weight?->sale?->item;].
Match Expressions
When testing a field that changes response based on the input provided, users previously had to use a switch() system, which required each entry to be assigned a case and result variable. That process is now condensed in PHP 8, with the use of a match() expression. Each input value is placed on the left, then is assigned a value as a result on the right, with the separation operator being => . For example, 'apple' => "fruit!", 'carrot' => "vegetable!"
Summary
There are numerous other changes in PHP 8 that were not covered in this documentation that are, being honest, a bit beyond my current understanding of PHP, such as, but not limited to, union types, attribute changes, named arguments, consistent type errors, new Just-In-Time compilation engines, and a couple handfuls of other fixes and improvements.