What is new in PHP 8
PHP 8 is the latest major release of the PHP programming language, released on November 26, 2020. PHP 8 comes with many new features, improvements, and bug fixes that make it a significant upgrade from its previous version, PHP 7.4. Here are some of the key features and improvements of PHP 8.
Table of Contents
JIT Compiler
PHP 8 introduces a Just-In-Time (JIT) compiler, which can improve the performance of PHP applications significantly by compiling code at runtime. The JIT compiler is disabled by default, but you can enable it by setting the “opcache.jit_buffer_size” directive in the php.ini file.
opcache.enable=1
opcache.jit_buffer_size=100M
Union Types
Union types allow developers to declare multiple types for a parameter or return type. For example, you can now declare a function that accepts either an integer or a string as a parameter.
function printValue(int|string $value) {
echo $value;
}
Named Arguments
Named arguments allow developers to pass arguments to a function using their parameter names, rather than their positions. This can make function calls more readable and reduce the likelihood of errors.
function createUser(string $username, string $email, int $age) {
// Create a user with the given properties
}
createUser(username: 'john_doe', email: 'john@example.com', age: 25);
Attributes
Attributes are a way to add metadata to classes, functions, and parameters. They can be used to provide additional information to IDEs, code analysis tools, and other tools that work with PHP code.
#[Route('/users/{id}', methods: ['GET'])]
function getUser(int $id) {
// Retrieve a user with the given ID
}
Constructor Property Promotion
Constructor property promotion is a new syntax that allows developers to define and initialize class properties directly in the constructor, rather than using separate property declarations and assignments.
class User {
public function __construct(
public string $name,
public string $email,
protected int $age = 0
) {}
}
$user = new User(name: 'John Doe', email: 'john@example.com');
Match Expression:
The match expression is a new control structure that allows developers to compare a value against a set of possible values and execute code based on the matching value. It is similar to the switch statement, but with a more concise syntax.
switch ($status) {
case 'active':
echo 'User is active';
break;
case 'inactive':
echo 'User is inactive';
break;
case 'blocked':
echo 'User is blocked';
break;
default:
echo 'Unknown status';
}
// Equivalent code using match expression:
match ($status) {
'active' => echo 'User is active',
'inactive' => echo 'User is inactive',
'blocked' => echo 'User is blocked',
default => echo 'Unknown status',
};
Nullsafe Operator
The nullsafe operator is a new operator that allows developers to safely access properties and methods of an object that may be null, without having to use multiple levels of null checks.
$user = getUserById($userId);
// Without nullsafe operator:
if ($user !== null && $user->getProfile() !== null && $user->getProfile()->getBio() !== null) {
echo $user->getProfile()->getBio();
}
// With nullsafe operator:
echo $user?->getProfile()?->getBio();
Improvements to Type System: PHP 8 also introduces improvements to its type system, including the ability to specify more precise return types, the ability to use mixed type for functions that can accept any type of input, and improvements to the handling of non-capturing catches.
Overall, PHP 8 is a major upgrade to the PHP programming language, offering developers a range of new features and improvements that can help them write more efficient and readable code.
Find more details at: https://www.php.net/releases/8.0/en.php