php version guide
PHP 7.4 Changes
PHP 7.4 brought typed properties, arrow functions, null-coalescing assignment, and array unpacking. It also deprecated curly-brace array and string offsets, which commonly appear in older projects.
Changes Worth Recognising
- Typed properties make uninitialised state visible; they are not automatically
null. - Arrow functions capture outer variables by value.
??=assigns a default only when the target is absent ornull.- Curly-brace offsets such as
$name{0}need replacement with square brackets.
Representative Code
PHP example
<?php
$products = [
['name' => 'Notebook', 'price' => 500],
['name' => 'Pen', 'price' => 150],
];
$names = array_map(fn(array $product): string => $product['name'], $products);
echo implode(', ', $names) . PHP_EOL;
// Prints:
// Notebook, Pen
Upgrade Review
- Initialise typed properties before reading them.
- Search for curly-brace offsets before upgrading.
- Check arrow functions that need mutation: capture-by-value may not match the intended behaviour.
Typed properties are the most consequential change here because they expose previously hidden object-state errors.
Practice
Modernise a Mapping Callback
Replace an anonymous callback used only to read $product["name"] with an arrow function. Then identify one situation where the longer closure remains clearer.