Camkode
Camkode

What’s New in PHP 8.4

Posted by Kosal

What’s New in PHP 8.4

PHP 8.4 is a major milestone in the evolution of the language. Packed with new features, performance improvements, and important deprecations, this release focuses on developer experience, code clarity, and future-proofing PHP applications.

Let’s dive into the most important changes and what they mean for you.

1. Property Hooks: Smarter Properties

Property Hooks introduce native getters and setters directly in the class property declaration. This significantly reduces boilerplate code and improves readability.

Before PHP 8.4

class User {
    private string $name;
    private int $age;

    public function setName(string $name): void {
        $this->name = ucfirst(trim($name));
    }

    public function setAge(int $age): void {
        if ($age < 0) {
            throw new InvalidArgumentException("Age cannot be negative.");
        }
        $this->age = $age;
    }

    public function getProfile(): string {
        return "{$this->name} is {$this->age} years old.";
    }
}

In PHP 8.4

class User {
    public string $name {
        set(string $value) => $this->name = ucfirst(trim($value));
    }

    public int $age {
        set(int $value) {
            if ($value < 0) {
                throw new InvalidArgumentException("Age cannot be negative.");
            }
            $this->age = $value;
        }
    }

    public string $profile {
        get => "{$this->name} is {$this->age} years old.";
    }

    public function __construct(string $name, int $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

Property hooks allow dynamic behavior while keeping your code clean and IDE-friendly — no more manually defined getters/setters everywhere.

2. Asymmetric Visibility: Read Public, Write Private

PHP 8.4 lets you define different visibility scopes for reading and writing a property. For example, a property can be publicly readable but only writable within the class.

class PhpVersion {
    public private(set) string $version = '8.4';

    public function increment(): void {
        [$major, $minor] = explode('.', $this->version);
        $this->version = "{$major}." . ((int)$minor + 1);
    }
}

Reduces boilerplate accessors and enforces stronger encapsulation.

3. #[\Deprecated] Attribute

PHP 8.4 introduces a native #[\Deprecated] attribute, giving developers the ability to mark their own methods or constants as deprecated.

class PhpVersion {
    #[\Deprecated(
        message: "Use getVersion() instead",
        since: "8.4"
    )]
    public function getPhpVersion(): string {
        return $this->getVersion();
    }
}

IDEs and static analysis tools can now catch deprecations early, just like internal PHP warnings.

4. New HTML5-Compliant DOM API

PHP 8.4 introduces a spec-compliant DOM API under the Dom\HTMLDocument namespace.

$dom = Dom\HTMLDocument::createFromString('<main><article class="featured">PHP 8.4!</article></main>');
$node = $dom->querySelector('main > article');
var_dump($node->classList->contains("featured")); // true

Cleaner API, better standards support, and improved HTML5 parsing.

5. Object API for BCMath

BCMath now supports OOP syntax through the BcMath\Number class.

use BcMath\Number;

$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2;

echo $result; // '2.12345'

Arithmetic operations feel more natural and modern, plus they support operator overloading.

6. New array_*() Utility Functions

PHP 8.4 adds powerful array helper functions:

  • array_find()
  • array_find_key()
  • array_any()
  • array_all()
$animal = array_find(['dog', 'cat', 'cow'], fn($v) => str_starts_with($v, 'c'));
echo $animal; // "cat"

These improve readability and reduce the need for custom loops.

7. PDO Driver-Specific Subclasses

PHP 8.4 introduces subclasses like Pdo\Sqlite, Pdo\Mysql, etc., allowing access to driver-specific methods directly.

$connection = PDO::connect('sqlite:foo.db'); // Returns an instance of Pdo\Sqlite
$connection->createFunction('prepend_php', fn($s) => "PHP $s");

Type-safe PDO usage and cleaner access to driver-specific features.

8. Cleaner Object Instantiation

You can now call a method directly on a new object without extra parentheses.

var_dump(new PhpVersion()->getVersion()); // PHP 8.4

Just syntactic sugar, but reduces visual clutter.

🆕 9. Other Notable Additions

  • Lazy Objects for deferring construction.
  • Improved JIT using IR framework.
  • New rounding functions: bcceil(), bcfloor(), bcround(), etc.
  • New string helpers: mb_trim(), mb_ucfirst(), etc.
  • Datetime improvements: microsecond precision support.
  • Better reflection and PCNTL capabilities.
  • HTML5-compliant XML/DOM handling.

🚫 Deprecations & Breaking Changes

  • Removed extensions: imap, oci8, pdo_oci, pspell — now on PECL.
  • Deprecated implicit nullable parameters.
  • Deprecated _ as a class name.
  • GMP class is now final.
  • mysqli_ping() and similar methods are deprecated.
  • Exit behavior is now more predictable.

PHP 8.4 is a significant upgrade, especially for those who value clean, modern, and expressive syntax. With property hooks, asymmetric visibility, DOM improvements, and object-oriented math operations, PHP continues to evolve into a more powerful and developer-friendly language.

Ready to upgrade? Make sure your code is compatible and enjoy the new features.

Upgrade to PHP 8.4 now!