Some improvements of PHP version 8
- 25-07-2022
- Toanngo92
- 0 Comments
PHP 8.0 was released on November 26, 2020. These changes make PHP easier and uncomplicated to work with. PHP 8.0 is considered a major update of the PHP language. It has many new features and optimizations including:
- Named arguments
- Attributes
- Constructor property promotion
- Union types
- Match expression
- Nullsafe operator
- Enhanced error handling, and type system
- Consistency
Mục lục
Named Arguments (Named Arguments)
In this new feature, users only have to specify required parameters and omit optional parameters. Arguments are self-documented and do not depend on sort order.
Example of difference between PHP 7.0 and 8.0
PHP 7 | PHP 8 |
htmlspecialchars($string, ENT_COMPATCT | ENT HTML401, ‘UTF-8’,false); | htmlspecialchars($string,double encode: false); |
Attributes (attributes)
Users can now use structured metadata with PHP’s native syntax, instead of PHP Document comments.
PHP 7.0 | PHP 8.0 |
class Postscontroller{ /** * @Route(“api/posts/{id}”),methods={“GET”} */ public function get($id){ // todo } } | class PostsController{ #[Route(“api/posts/{id}”, ,methods: [“GET”])] public function get($id){ // todo } } |
Constructor property promotion (introduction to object initialization)
There is shortening of the source code when defining and initializing properties for objects.
PHP 7.0 | PHP 8.0 |
class Point{ public float $x; public float $y; public float $z; public function __construct( float $x = 0.0, float $y = 0.0, float $z = 0.0 ){ $this->x = $x; $this->y = $y; $this->z = $z; // todo } } | class Point{ public function __construct( public float $x = 0.0, public float $y = 0.0, public float $z = 0.0 ){ //todo } } |
Union types (combining data types)
Users can use runtime-validated native associative type declarations, instead of PHP Docs annotations.
PHP 7 | PHP 8 |
class Number{ /** @var int|float */ private number; /** * @param float|int $number */ public function __construct($number){ $this->number = $number; } } new Number(‘NaN’); // its work | class Number { public function __construct(private int|float $number){ // to do } } new Number(‘NaN’); // type error |
Match Expression (match expressions)
Expression matching in PHP 8.0 is similar to conversion with the following features:
- A match is an expression, which means its result can be stored in a variable or it can be returned.
- Matching branches only support single-line expressions and do not require a break statement; .
- The comparison does not strictly compare.
PHP 7 | PHP 8 |
<?php switch(8.0){ case ‘8.0’: $result = ‘8.0’; break; case 8.0; $result = ‘8.0 expected’; break; } echo $result; // 8.0 | echo match(8.0){ ‘8.0’ => ‘8.0’, 8.0 => ‘8.0 expected’, }; // 8.0 excepted |
Null Safe operator
The user can use a series (chain) of calls with the new null-safe operator instead of
write code for conditions that check for null.
The entire execution is aborted when the identification of an element in the sequence fails, and thereafter, the entire chain is null.
PHP 7 | PHP 8 |
$country = null; if($session !==null){ $user = $session->user; if($user !==null){ $address = $user->getAddress(); } if($address !==null){ $country = $address->country; } } | $country = $session?->user?->getAddress()?->country; |
Enhanced Error Handling and Type System
There have been many changes in different types of systems and improved error handling as follows:
- Stricter type checking for arithmetic/bitwise operators than previous version
- Validation of abstract trait method
- Magic methods now have correct signatures.
- Warning engine has been reclassified
- Incompatible method causes fatal error
- Inheritance with private methods
- Mixed type
- Static return type
- Use Opaque objects instead of resources for OpenSSL, GD, Curl,
XML Writer, Sockets and XML Extensions
Consistency (consistency)
PHP 8.0 has a consistency error for internal functions. Most of the inner functions throw an Error exception when parameters cannot be validated.
Just In Time (JIT) Compiler
As an interpreted or interpreted language, PHP, when compiled, does not run immediately at launch time. It runs in real time. In previous versions of PHP, during compilation, whenever PHP code was run, the interpreter had to first interpret, then compile, and finally execute the code. This is done repeatedly for each request, resulting in slow code execution and waste of CPU resources.
This problem has now been fixed with the introduction of the Just In Time (JIT) compiler, in PHP 8.0.
The JIT compiler allows users to compile a program into machine code just before it can be executed. As JIT skips the compilation phase, it provides flexibility in PHP code and delivers far-reaching improvements in code execution, memory usage, and performance. However, these improvements only apply to mathematical or numerical calculations, not regular PHP Web applications. JIT is widely used in programs that involve long execution cycles such as 3D rendering, data analysis or Artificial Intelligence.
PHP 8.0 introduces two JIT compilation tools, namely Tracing JIT and Function JIT.
Tracing JIT is the most secure of the two. On synthetic benchmarks, it has three times better performance. In addition, there are two improvements on specific long running applications.