What’s New in PHP 7.4? Typed Properties, Arrow Functions, and More

PHP 7.4 introduces a suite of powerful features—including typed properties, arrow functions, limited covariance/contravariance, array unpacking, numeric literal separators, short lambda syntax, weak references, throwable __toString() and Opcache preloading—each illustrated with concise code examples for modern backend development.

21CTO
21CTO
21CTO
What’s New in PHP 7.4? Typed Properties, Arrow Functions, and More

The most widely used web development language, PHP, has released version 7.4.0, the fourth feature update of the PHP 7 series, bringing many improvements and new capabilities.

Typed Properties

Class properties now support explicit type declarations, preventing assignment of incompatible values.

class Employee {
    protected Employee $manager = null;
    protected string $firstName;
    protected string $lastName;
    protected int $employeeId;

    public function __construct(int $id, string $first, string $last) {
        $this->employeeId = $id;
        $this->firstName = $first;
        $this->lastName = $last;
    }
}

Arrow Functions

Arrow functions provide a concise syntax for defining functions with implicit lexical scope binding.

<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
?>

Limited Return Type Covariance and Argument Type Contravariance

Full covariance/contravariance support is available only when using autoloading; non‑circular type references must be available before use.

<?php
class A {}
class B extends A {}

class Producer {
    public function method(): A {}
}
class ChildProducer extends Producer {
    public function method(): B {}
}
?>

Unpacking Inside Arrays

<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// Result: ['banana', 'orange', 'apple', 'pear', 'watermelon'];
?>

Numeric Literal Separator

Underscores can be placed between digits in numeric literals for readability.

<?php
6.674_083e-11; // float
299_792_458;   // decimal
0xCAFE_F00D;   // hexadecimal
0b0101_1111;   // binary
?>

Short Lambda Syntax

Similar to JavaScript, Python, or Rust, PHP now allows one‑line anonymous functions.

$arr = [1, 2, 3, 4, 5];
$factor = 5;
array_map(function(int $item) use ($factor) {
    return $item * $factor;
}, $arr);

array_map(fn($x) => $x * $factor, $arr);

Weak References

Weak references let developers hold references to objects without preventing their destruction.

Allow Exceptions from __toString()

It is now possible to throw exceptions from __toString(); previously this caused a fatal error, but now such errors are converted to Error exceptions.

Opcache Preloading

Opcache now supports preloading, allowing scripts to be loaded into memory before handling requests.

Additional deprecations and removal of some extensions are also included; see the official migration guide for details.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPCovarianceArrow Functionsweak referencesTyped Properties
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.