Preview of PHP 8.4 New Features
ServBay
Posted on May 18, 2024
New Features in PHP 8.4
PHP 8.4 is set to be released on November 21, 2024. It will include property hooks, JIT improvements, and method chaining without the need for additional parentheses. This is a significant change!
Property Hooks RFC
One of the biggest changes in modern PHP history: the ability to define property hooks.
class BookViewModel
{
public function __construct(
private array $authors,
) {}
public string $credits {
get {
return implode(', ', array_map(
fn (Author $author) => $author->name,
$this->authors,
));
}
}
public Author $mainAuthor {
set (Author $mainAuthor) {
$this->authors[] = $mainAuthor;
$this->mainAuthor = $mainAuthor;
}
get => $this->mainAuthor;
}
}
The goal of property hooks is to remove the need for numerous getters and setters by allowing each property to define its own get and set hooks. Hooks are optional, and you don’t need to add both hooks for a specific property. For instance, a property with only a get hook is a virtual property. This is probably the biggest update in PHP 8.4 so far, and it's highly anticipated as it reduces a lot of boilerplate code.
interface HasAuthors
{
public string $credits { get; }
public Author $mainAuthor { get; set; }
}
New Instance Method Chaining Without Parentheses RFC
If property hooks weren't enough, PHP 8.4 has another feature that saves a lot of code: you no longer need to wrap new instance calls in parentheses to chain methods. Personally, I find this change quite nice, considering how often we had to do this:
$name = (new ReflectionClass($objectOrClass))->getShortName();
Now in PHP 8.4, you can just do this:
$name = new ReflectionClass($objectOrClass)->getShortName();
JIT Changes RFC
PHP 8.4 changes how JIT is enabled. Before PHP 8.4, you had to set opcache.jit_buffer_size to 0 to disable JIT, but now you can disable it like this:
opcache.jit=disable
opcache.jit_buffer_size=64m
The only way users will be affected by this change is if they have specified opcache.jit_buffer_size without opcache.jit. In such cases, you will need to add opcache.jit=tracing to re-enable JIT.
Additionally, JIT has been improved to run faster and use less memory in certain scenarios.
Implicit Nullable Types Deprecation
PHP has a quirky behavior where typed variables with a default null value automatically become nullable types:
function save(Book $book = null) {}
This behavior is now deprecated and will be removed in PHP 9. The solution is to explicitly make Book a nullable type:
function save(?Book $book = null) {}
New DOM HTML5 Support RFC
PHP 8.4 adds a \Dom\HTMLDocument
class that can correctly parse HTML5 code. The old \DOMDocument
class is still available for backward compatibility.
$doc = \Dom\HTMLDocument::createFromString($contents);
Early Access to PHP 8.4 (Dev)
As a one-stop web development environment tool, ServBay provides developers with an exclusive opportunity to explore and experiment with the latest features that PHP 8.4 has to offer. This early access is invaluable for developers looking to innovate and leverage new functionalities in their projects ahead of the curve. By having early access to PHP 8.4, ServBay users can start incorporating the latest PHP advancements into their projects, explore new development methodologies, and ultimately deliver more innovative and robust web applications.
If you want to get the latest information, follow X(Twitter) and Facebook
If you have any questions, our staff would be pleased to help, just join our Discord
Posted on May 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.