Middle
What new features appeared in PHP 7.4?
sobes.tech AI
Answer from AI
- Introduction of typed class properties.
- Addition of arrow functions (short syntax for lambda functions).
- Null coalescing assignment operator (
??=). - Adding WeakReference.
- Preloading for improved application performance.
- Covariance of return types and contravariance of function arguments.
- Spread operator (
...) syntax in array expressions. unserialize()now throwsThrowable.- Performance improvements, especially in FFI (Foreign Function Interface).
- Deprecation of some features.
<?php
// Example of a typed property
class User
{
public int $id;
public string $name;
}
// Example of an arrow function
$numbers = [1, 2, 3, 4];
$squared = array_map(fn($n) => $n * $n, $numbers);
// Example of null coalescing assignment operator
$data = ['value' => null];
$data['value'] ??= 'default'; // $data['value'] will become 'default'
// Example of spread operator in arrays
$array1 = [1, 2];
$array2 = [3, 4];
$merged = [...$array1, ...$array2]; // $merged will be [1, 2, 3, 4]