PHP 8.4 is on its way, and it’s arriving on November 21, 2024! This latest version is packed with new and exciting features that are sure to make your coding life easier. Whether you’re a seasoned PHP developer or just getting started, these updates are designed to streamline your workflow and cut down on boilerplate code. Let’s dive into what’s new in PHP 8.4.
Property Hooks: A Game-Changer
One of the most buzz-worthy features in PHP 8.4 is property hooks. This new addition allows you to define get and set hooks directly in your property definitions. Say goodbye to repetitive getter and setter methods!
Here’s a sneak peek:
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;
}
}
This feature enables each property to define its own get and set hooks. It’s optional, meaning you can choose to define only a get hook if that suits your needs. This creates what’s called a virtual property.
The best part? Property hooks can even be defined in interfaces:
interface HasAuthors
{
public string $credits { get; }
public Author $mainAuthor { get; set; }
}
Stay tuned! We’ll be diving deeper into property hooks in a follow-up post soon.
Chaining Methods Without Parentheses
Another fantastic addition to PHP 8.4 is the ability to chain methods on new objects without using additional parentheses. Imagine writing code without unnecessary brackets cluttering it up!
Before:
$name = (new ReflectionClass($objectOrClass))->getShortName();
Now:
$name = new ReflectionClass($objectOrClass)->getShortName();
This feature isn’t just limited to methods. You can chain properties, static methods, constants, and more. It’s a small change, but one that will make your code much cleaner and easier to read.
JIT Improvements
PHP 8.4 brings several enhancements to Just-In-Time (JIT) compilation. The way you enable JIT has changed for the better. Before, you had to set opcache.jit_buffer_size
to 0 to disable it. Now, it’s as simple as this:
opcache.jit=disable
opcache.jit_buffer_size=64m
The only thing to watch out for is if you previously set a opcache.jit_buffer_size
but no opcache.jit
. In that case, you’ll need to specify opcache.jit=tracing
to enable JIT again.
What’s more, the JIT improvements in PHP 8.4 make it run faster in certain cases and use less memory. This means better performance and efficiency for your applications.
Deprecating Implicit Nullable Types
A rather peculiar behavior in PHP has been the implicit marking of a parameter as nullable if it had a null
default value. That’s changing in PHP 8.4. Here’s what it looked like:
function save(Book $book = null) {}
// Deprecated: Implicitly marking parameter $book as nullable is deprecated,
// the explicit nullable type must be used instead
To make it clear and explicit, you now need to declare it as nullable:
function save(?Book $book = null) {}
This change is important for clarity and will be crucial when PHP 9 rolls around, as this implicit behavior will be removed entirely. Keep this in mind to future-proof your code!
New DOM HTML5 Support
PHP 8.4 is also adding HTML5 support to its DOMDocument
class. A new \Dom\HTMLDocument
class will be available to parse HTML5 code properly. For backwards compatibility, the old \DOMDocument
class remains available
Here’s a quick example:
$doc = \Dom\HTMLDocument::createFromString($contents);
This addition ensures that you can handle the latest HTML standards with ease.
Wrapping Up
PHP 8.4 is packed with features designed to make your coding more efficient and modern. Property hooks eliminate the need for boilerplate getter and setter methods, while method chaining without additional parentheses cleans up your code. JIT improvements ensure better performance, and explicit nullable types bring clarity to your function definitions. Plus, new DOM HTML5 support means you can stay up-to-date with the latest web standards.
Stay tuned for more updates as PHP 8.4 continues to develop. If you want to stay in the loop, make sure to subscribe to our blog. Exciting times are ahead for PHP developers!