Middle
What new features appeared in PHP version 7.4?
sobes.tech AI
Answer from AI
- Typed class properties.
- Arrow functions (short syntax for anonymous functions).
- Null coalescing assignment operator (
??=). - Preloading for performance improvement.
- Spread operator in arrays (
...). - Weak references for memory management.
- Limited covariance and contravariance of types.
- CFI (Custom ini files) — support for user-defined ini files.
- Deprecation of some functions and features (e.g., aliases
is_countable,FILTER_SANITIZE_MAGIC_QUOTES, etc.). - Improvements in FFI (Foreign Function Interface).
- Thousands separator in numeric literals (
1_000_000).
<?php
// Typed property
class User
{
public string $name;
public ?int $age; // Can be null
}
// Arrow function
$func = fn(int $a, int $b) => $a + $b;
echo $func(1, 2); // 3
// Null coalescing assignment operator
$data = [
'key' => null,
];
$value ??= 'default';
echo $value; // default
// Spread operator in arrays
$arr1 = [1, 2];
$arr2 = [3, 4];
$merged = [...$arr1, ...$arr2]; // [1, 2, 3, 4]
<?php
// WeakReference
$obj = new stdClass;
$weakref = WeakReference::create($obj);
unset($obj); // Object removed
// Check
var_dump($weakref->get()); // null