Posted by Kosal
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.
Property Hooks introduce native getters and setters directly in the class property declaration. This significantly reduces boilerplate code and improves readability.
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.";
}
}
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.
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.
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.
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.
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.
array_*()
Utility FunctionsPHP 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.
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.
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.
bcceil()
, bcfloor()
, bcround()
, etc.mb_trim()
, mb_ucfirst()
, etc.imap
, oci8
, pdo_oci
, pspell
— now on PECL._
as a class name.final
.mysqli_ping()
and similar methods are deprecated.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.