New Features in PHP 7.4: A Practical Guide with Code Examples
`PHP 7.4`, released in November 2019, brought a host of new features, syntax improvements, and performance enhancements. As the last major release before `PHP 8`, it serves as a crucial stepping stone for developers aiming to modernize their codebase. Below, we explore the most impactful additions, complete with code examples to help you get started.
Arrow Functions (Short Closures)
Arrow functions offer a concise syntax for writing anonymous functions, ideal for one-liner operations such as those used in array mapping or filtering. They automatically capture variables from the parent scope, eliminating the need for the use keyword.
// Traditional anonymous function $ids = array_map(function ($user) { return $user->id; }, $users); // Arrow function (PHP 7.4+) $ids = array_map(fn($user) => $user->id, $users);
Arrow functions can also use type hints:
$ids = array_map(fn(User $user): int => $user->id, $users);
Typed Properties
Class properties can now be type-hinted, allowing the `PHP` engine to enforce type safety at the property level. This helps catch bugs early and improves code clarity.
class User { public string $name; public ?DateTime $lastLogin; } $user = new User(); $user->name = 'Alice'; // OK $user->lastLogin = null; // OK, nullable type
Attempting to assign an incorrect type will result in a `TypeError`.
Spread Operator in Arrays
The spread operator (`...`) can now be used within array expressions, making it easier and more performant to merge arrays and unpack elements.
$numbers = [4, 5]; $scores = [1, 2, 3, ...$numbers]; print_r($scores); // [1, 2, 3, 4, 5]
You can use the spread operator anywhere in the array, not just at the end.
Null Coalescing Assignment Operator (`??=`)
This operator provides a shorthand for assigning a value to a variable only if it is `null`.
// Before PHP 7.4 $_GET['user'] = $_GET['user'] ?? 'nobody'; // PHP 7.4+ $_GET['user'] ??= 'nobody';
This simplifies code that sets default values.
Weak References
Weak references allow you to reference an object without preventing it from being garbage collected. This is particularly useful for implementing caches or managing memory in long-running scripts.
$object = new stdClass; $weakRef = WeakReference::create($object); var_dump($weakRef->get()); // object(stdClass) unset($object); var_dump($weakRef->get()); // NULL
Additional Noteworthy Features
- Preloading: Improves performance by loading `PHP` files into memory on server startup.
- FFI (Foreign Function Interface): Allows `PHP` to call `C` functions and access `C` data structures.
- Numeric Literal Separator: You can use underscores in numeric literals for readability (e.g., `1_000_000`).
- Array Merge Enhancements: `array_merge()` and `array_merge_recursive()` now allow empty parameter lists, returning an empty array if no arguments are provided.
- Deprecations: Curly braces for array and string offset access are deprecated, as are several legacy functions and features.
Conclusion
`PHP 7.4` delivers meaningful improvements that enhance code readability, performance, and safety. By adopting these features, developers can write cleaner, more modern `PHP` and prepare their projects for the transition to `PHP 8` and beyond.
0%