What's New in PHP 8.4: A Comprehensive Guide

What's New in PHP 8.4: A Comprehensive Guide

Ihor (Harry) Chyshkala
Ihor (Harry) ChyshkalaAuthor
|3 min read

PHP 8.4, scheduled for release on November 21, 2024, brings several groundbreaking features that significantly enhance developer productivity and code clarity. Let's dive deep into the major changes and understand how they'll transform PHP development.

Property Hooks: A Revolution in Property Management

One of the most significant additions in PHP's history is the introduction of property hooks. This feature fundamentally changes how we handle class properties by eliminating verbose getter and setter boilerplate code.

Consider this common scenario in pre-8.4 PHP:

php(18 lines)
1class BookViewModel
2{
3    private array $authors = [];
4    
5    public function getCredits(): string 
6    {
7        return implode(', ', array_map(
8            fn (Author $author) => $author->name,

With PHP 8.4's property hooks, this becomes much more elegant:

php(24 lines)
1class BookViewModel
2{
3    public function __construct(
4        private array $authors,
5    ) {}
6
7    public string $credits {
8        get {

Property hooks offer several advantages:

  • Virtual properties through get-only hooks
  • Computed properties without method calls
  • Interface support for property definitions
  • Clear separation of concerns

Asymmetric Visibility: Fine-grained Access Control

PHP 8.4 introduces asymmetric visibility, allowing different access levels for reading and writing properties. This feature is particularly valuable for creating immutable objects and enforcing encapsulation.

php
1class BookViewModel
2{
3    // Public read, private write
4    private(set) Author $author;
5    
6    // Public read, protected write
7    public protected(set) Author $editor;
8    
9    // Works with constructor promotion too
10    public function __construct(
11        private(set) Author $publisher
12    ) {}
13}

This feature elegantly solves the common pattern of "read-only from outside, writable from inside" without requiring separate getters and setters.

Simplified Method Chaining

PHP 8.4 removes a long-standing annoyance by allowing method chaining on new instances without extra parentheses. This seemingly small change makes code more readable and reduces syntax noise.

php
1// Before PHP 8.4
2$name = (new ReflectionClass($objectOrClass))->getShortName();
3
4// PHP 8.4
5$name = new ReflectionClass($objectOrClass)->getShortName();

This improvement applies to all forms of member access:

  • Method calls
  • Property access
  • Static method calls
  • Constant access

Enhanced Array Functions

PHP 8.4 introduces several useful array functions that developers have long relied on third-party libraries to provide:

php(17 lines)
1// Find first matching element
2$longTitle = array_find(
3    $posts, 
4    fn (Post $post) => strlen($post->title) > 50
5);
6
7// Check if any element matches
8$hasPublished = array_any(

Modern HTML5 Support

The new \Dom\HTMLDocument class brings proper HTML5 parsing capabilities to PHP:

php
1$doc = \Dom\HTMLDocument::createFromString($contents);
2// Now properly handles modern HTML5 elements and syntax

This addition maintains backward compatibility while providing modern HTML processing capabilities.

BCMath Object API

PHP 8.4 introduces an object-oriented interface for BCMath operations with operator overloading support:

php
1use BCMath\Number;
2
3$price = new Number('19.99');
4$quantity = new Number('2');
5$total = $price * $quantity;
6
7echo $total->value; // '39.98'

Improved Deprecation Handling

The new #[Deprecated] attribute provides a standardized way to mark code as deprecated:

php
1#[Deprecated(
2    "Use newFunction() instead",
3    since: "tempest/framework:1.1"
4)]
5function oldFunction() {
6    // ...
7}

Other Notable Improvements

DateTime Enhancements

php
1// New static constructors
2$date = DateTime::createFromTimestamp(1234567890);
3
4// Microsecond precision
5$date->setMicrosecond(123456);
6$microseconds = $date->getMicrosecond();

Lazy Objects

php
1$initializer = static function (MyClass $proxy): MyClass {
2    return new MyClass(123);
3};
4$object = (new ReflectionClass(MyClass::class))->newLazyProxy($initializer);

JIT Improvements

bash
1# New JIT configuration
2opcache.jit=disable
3opcache.jit_buffer_size=64m

Breaking Changes and Deprecations

Several backwards-incompatible changes are introduced:

  • E_STRICT constant deprecation
  • Implicit nullable types removal
  • GMP class now final
  • Various DOM element property deprecations
  • Session configuration changes

Preparing for the Update

To ensure a smooth transition to PHP 8.4:

  1. Update nullable type hints to use explicit notation
  2. Review usage of deprecated DOM properties
  3. Update JIT configurations if using custom settings
  4. Test thoroughly with the new property hooks system
  5. Consider adopting asymmetric visibility for better encapsulation

PHP 8.4 represents a significant step forward in PHP's evolution, introducing features that make code more expressive and maintainable while removing long-standing pain points. The additions of property hooks and asymmetric visibility in particular show PHP's continued commitment to modern programming paradigms while maintaining its pragmatic approach to web development.

About the Author

Ihor (Harry) Chyshkala

Ihor (Harry) Chyshkala

Code Alchemist: Transmuting Ideas into Reality with JS & PHP. DevOps Wizard: Transforming Infrastructure into Cloud Gold | Orchestrating CI/CD Magic | Crafting Automation Elixirs