PHP 8.4 was released on November 21, 2024. It contains some exiting new features.
PHP 8.4 introduces property hooks, marking one of the biggest shifts in how properties are managed in PHP. This feature lets developers attach “get” and “set” hooks directly to properties, reducing boilerplate code.
class Product
{
public function __construct(private float $basePrice) {}
public string $price {
get => '$' . number_format($this->basePrice, 2);
set (float $newPrice) {
if ($newPrice < 0) throw new InvalidArgumentException("Price cannot be negative.");
$this->basePrice = $newPrice;
}
}
}
In PHP, managing property access has always posed challenges, especially when balancing readability and immutability. PHP 8.4’s asymmetric visibility solves this by allowing different access levels for reading and modifying properties.
class Example
{
public private(set) string $status = 'active';
}
PHP 8.4 introduces four new array functions for simpler searching and validation.
Here’s a quick look at what each function does:
array_find
: Returns the first value that satisfies the callback.array_find_key
: Returns the key of the first item that matches the callback.array_any
: Returns true
if at least one item in the array passes the callback check.array_all
: Returns true
only if all items in the array pass the callback check.With PHP 8.4, a new \Dom\HTMLDocument
class enables easier HTML5 parsing, solving issues that existed with \DOMDocument
.
$htmlContent = '<!DOCTYPE html><html><body><p>Hello, World!</p></body></html>';
$doc = \Dom\HTMLDocument::createFromString($htmlContent);
PHP 8.4 improves method chaining by removing the need for parentheses around class instantiation, making code more readable.
$user = (new User())->setName('Alice')->setEmail('[email protected]');
Now, you can write it without parentheses:
$user = new User()->setName('Alice')->setEmail('[email protected]');
Yes, it will! Laravel 11 supports PHP 8.2 and higher, which means PHP 8.3 and PHP 8.4 are supported as well.
Websites will go down, and errors will happen. With Sorane, you are sure to catch any errors and performance issues the moment they happen.
Takes 1 minute to get started.
No credit card required. Cancel anytime.