PHP 7.4: Typed Properties at Last
· Jerwin Arnado
Archive note: this is a backdated post, written years later while rebuilding this site. It’s dated to the moment it covers, but the hindsight is real.
PHP 7.4 landed on November 28, and it delivers the feature I’ve wanted since I learned what a docblock was lying about: typed properties.
Typed properties: the docblock retirement program
Every serious PHP codebase carries thousands of lines like this:
class Invoice
{
/** @var int */
public $amount;
/** @var Customer|null */
public $customer;
}
The types live in comments — read by IDEs, enforced by nothing. Assign a string to $amount and PHP shrugs; the bug surfaces three layers later wearing a disguise. As of 7.4:
class Invoice
{
public int $amount;
public ?Customer $customer;
}
Real types, engine-enforced, TypeError at the moment of the mistake instead of an oddity in production. Method signatures got types back in 7.0; properties were the gap where the lies lived. The gap is closed. For those of us building business apps where $amount being an honest integer is the entire job, this is the most important PHP feature in years — not glamorous, just load-bearing.
Arrow functions: the ceremony trim
Single-expression closures lose their boilerplate, and — the actual gift — capture outer scope automatically, retiring a thousand use clauses:
// before
$discounted = array_map(function ($price) use ($rate) {
return $price * $rate;
}, $prices);
// now
$discounted = array_map(fn ($price) => $price * $rate, $prices);
Collection-heavy Laravel code is about to get visibly shorter. My right pinky, which has typed use ( more than any other sequence, sends regards.
The quieter ledger
- Preloading — opcache can load chosen files into memory once at server start, shared across requests. Frameworks are the obvious beneficiary; real-world gains will vary, but free single-digit percentages are still free.
- Null coalescing assignment —
$config['mode'] ??= 'default';— small, immediately everywhere. - Spread in arrays —
$all = [...$first, ...$second];— finally matching the function-call spread from 5.6. - Covariant return types and FFI (call C libraries from PHP — niche for web work, a door opener for the language’s range).
- The deprecation bell tolls for curly-brace array access (
$str{0}) and a pile of edge-case behaviors — the housecleaning before a major version, which tells you what’s next.
End of the 7.x road
This is the last 7.x release, and the line deserves a salute. PHP 7.0 (2015) made the language fast enough to silence a decade of jokes; 7.4 leaves it typed, expressive, and ready for whatever PHP 8 turns out to be — the rumor mill says a JIT compiler, which will make for an interesting post next year.
Between Laravel’s maturity push, Vapor’s serverless bet, and now a typed PHP, 2019 quietly closes as the year my unfashionable stack got its act fully together. The hipper ecosystems can keep their churn; I’ll be over here, deleting docblocks with a typed smile.