10 Must‑Know PHP 8.3 Features to Supercharge Your Code in 2024

This article introduces ten powerful PHP 8.3 updates—including readonly properties, enums, match expressions, constructor promotion, named arguments, nullsafe operator, union types, array unpacking, JSON error handling, and JIT compilation—explaining how each feature works and how it can boost development efficiency.

php Courses
php Courses
php Courses
10 Must‑Know PHP 8.3 Features to Supercharge Your Code in 2024

Whether you are an experienced expert or a newcomer to programming, the 2024 PHP updates bring significant benefits, optimizing your code and enhancing development efficiency. Let’s explore ten impactful new features that will transform your PHP development journey.

1. Readonly properties: assign only during initialization, immutable thereafter.

class User {
    public readonly string $username;

    public function __construct(string $username) {
        $this->username = $username;
    }
}

2. Enums: a set of named constants representing specific states or categories.

enum Status {
    case PENDING;
    case ACTIVE;
    case INACTIVE;
}
$status = Status::ACTIVE;

3. Match expressions: a modern, more flexible alternative to switch.

$status = 'active';
$message = match ($status) {
    'active'   => '用户处于活跃状态',
    'inactive' => '用户处于非活跃状态',
    'pending'  => '用户处于待定状态',
    default    => '状态未知',
};

4. Constructor property promotion: set property values directly in the constructor.

class Point {
    public function __construct(
        public float $x,
        public float $y
    ) {}
}
$point = new Point(1.5, 2.5);

5. Named arguments: pass values by parameter name, removing positional constraints.

function createUser(string $username, bool $isAdmin = false) {
    // your code here
}
createUser(username: 'john_doe', isAdmin: true);

6. Nullsafe operator: simplify null checks.

$user = getUser();
$profile = $user?->getProfile()?->getBio();

7. Union types: allow variables to accept multiple types.

function processNumber(int|float $number): int|float {
    return $number * 2;
}

8. Array unpacking with string keys: simplify array merging.

$array1 = ['a' => 1, 'b' => 2];
$array2 = ['c' => 3, ...$array1];
print_r($array2);
// Output: ['c' => 3, 'a' => 1, 'b' => 2]

9. JSON_THROW_ON_ERROR: automatically throw exceptions on JSON errors.

ini_set('json.exceptions', '1');

try {
    $data = json_decode('{"invalidJson":}', true);
} catch (JsonException $e) {
    echo 'JSON 错误: ' . $e->getMessage();
}

10. JIT compilation: just‑in‑time compile PHP code to boost script performance.

It is bundled with the opcache extension and can be enabled in php.ini.

zend_extension=php_opcache.dll
opcache.jit=1205          ; configuration using four digits OTRC
opcache.enable_cli=1     ; in order to work in the CLI as well
opcache.jit_buffer_size=128M ; dedicated memory for compiled code
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.

BackendNew Featurescode-examplesPHP8
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.