PHP 7: Maintaining and Upgrading Legacy Apps · PHP 7 Language Essentials
PHP 7.4: Typed Properties, Arrow Functions and Preloading
The last PHP 7 release and the features that bridge to PHP 8.
PHP 7.4 (2019) introduced typed properties, arrow functions (fn($x) => $x * 2), the spread operator in arrays, numeric literal separators (1_000_000), ??=, covariant returns/contravariant parameters, OPcache preloading and FFI. It deprecated nested ternaries without parentheses and curly-brace string offsets. Many legacy apps stopped at 7.4, which makes it the usual starting point for PHP 8 upgrades.
<?php
final class Invoice
{
public int $id;
public ?string $notes = null;
/** @var LineItem[] */
private array $items = [];
public function total(): int
{
return array_sum(array_map(fn(LineItem $i) => $i->priceCents * $i->qty, $this->items));
}
}
$all = [...$defaults, ...$overrides]; // spread in arrays (integer keys only in 7.4)
$limit = 10_000;
Common pitfalls
- Accessing a typed property before initialization throws
Error: must not be accessed before initialization. - Spreading arrays with string keys fails in 7.4 (allowed from 8.1).
- Unparenthesized nested ternaries — deprecated in 7.4 and a fatal error in 8.0.
Key points
- Typed properties and arrow functions
- Uninitialized typed properties throw
- 7.4 is the usual PHP 8 upgrade baseline
Further reading: PHP manual: PHP 7.4 new features
Practice questions
What happens if you read a typed property that was never assigned?
Answer
PHP throws an Error ('must not be accessed before initialization'). Untyped properties default to null, but typed ones start uninitialized unless they have a default.
Why is $a ? $b : $c ? $d : $e a problem when upgrading?
Answer
Unparenthesized nested ternaries were deprecated in 7.4 and became a compile error in PHP 8.0 because their left-associative behavior was confusing. Add parentheses or refactor.
Open in the app to mark lessons complete, take notes and track your progress.