PHP 8.4, set to be released on November 21, 2024, brings an exciting wave of new features, enhancements, and performance updates. From property hooks to enhanced HTML5 support, PHP 8.4 aims to simplify code and improve developer experience. Let’s dive into the most significant updates!
1. Property Hooks
Property hooks are a game-changer for modern PHP development. They allow developers to define custom getters and setters directly within properties, reducing boilerplate code dramatically.
Example:
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;
}
}
Key Points:
- Hooks can be defined using get and set directly in property declarations.
- Properties with only a get hook act as virtual read-only properties.
- Property hooks can also be defined in interfaces, making this a flexible addition.
interface HasAuthors
{
public string $credits { get; }
public Author $mainAuthor { get; set; }
}
2 Method Chaining with new Without Parentheses
PHP 8.4 simplifies the syntax for chaining methods or properties on a new instance. You no longer need parentheses for instantiation.
Before PHP 8.4:
$name = (new ReflectionClass($objectOrClass))->getShortName();
In PHP 8.4:
$name = new ReflectionClass($objectOrClass)->getShortName();
This improvement also works for properties, constants, and static methods, reducing clutter.
3 Asymmetric Visibility: Flexible Property Access
Asymmetric visibility allows you to set different access levels for reading and writing class properties. For example, a property can be public for reading but private for writing.
Example:
class BookViewModel
{
public private(set) Author $author;
}
Here, $author can be read publicly but can only be modified within the class.
Shorthand Option:
If the property is public by default, you can simply write:
private(set) Author $author; // Same as public private(set)
Asymmetric visibility also works for promoted properties:
public function construct(private(set) Author $author) {}
4 Array_find and Related Utility Functions
A new native function array_find() has been introduced to retrieve the first element in an array that matches a condition.
Example:
$posts = [...];
$firstMatch = array_find($posts, function (Post $post) { return strlen($post->title) > 5;
});
Additional Variants:
- array_find_key() – Find the key of the first matching element.
- array_any() – Check if any element matches a condition.
- array_all() – Verify that all elements match a condition.
5 Implicit Nullable Types Deprecated
PHP now enforces explicit nullable types. Previously, a parameter defaulting to null was automatically considered nullable.
Deprecated Syntax:
function save(Book $book = null) {}
Correct Syntax:
function save(?Book $book = null) {}
This change improves clarity and prepares PHP for stricter type handling in future versions.
6 HTML5 Support with \Dom\HTMLDocument
PHP 8.4 introduces a new HTML5 parser with the \Dom\HTMLDocument class, providing better support for modern HTML5 content.
Example:
$doc = \Dom\HTMLDocument::createFromString($contents);
The existing \DOMDocument class remains for backward compatibility.
7 JIT Improvements and Configuration Changes
PHP 8.4 updates the JIT (Just-In-Time) compiler with cleaner configuration options:
opcache.jit=disable
opcache.jit_buffer_size=64m
- You can now explicitly disable JIT with opcache.jit=disable.
- Performance and memory efficiency have also been improved.
8 Lazy Objects: Efficient Proxy Initialization
PHP 8.4 adds lazy object support, a common feature in frameworks for creating proxy objects on demand.
Example:
$initializer = static function (MyClass $proxy): MyClass { return new MyClass(123);
};
$reflector = new ReflectionClass(MyClass::class);
$object = $reflector->newLazyProxy($initializer);
This improves efficiency when working with large objects that don’t need immediate instantiation.
9 Exit and Die as Proper Functions
exit() and die() are now fully recognized as functions, supporting named arguments and strict types.
Example:
exit(message: "Goodbye");
The old keyword syntax exit; continues to work as usual.
10 Object API for BCMath
The BCMath extension now supports an object-oriented API with operator overloading, simplifying mathematical operations.
Example:
use BCMath\Number;
$num1 = new Number('2');
$num2 = new Number('3');
$result = $num1 + $num2;
echo $result->value; // Outputs "5"
Conclusion:
PHP 8.4 delivers powerful new features that streamline code, enhance performance, and simplify common tasks. From Property Hooks to the array_find utility and improved JIT configuration, developers have much to look forward to. Be sure to explore these changes and start preparing your code for the PHP 8.4 release!
Stay tuned for further updates as PHP continues to evolve into a modern and robust programming language.
At 200OK Solutions, we specialize in cutting-edge software development, tailored IT solutions, and expert technology consulting. As innovators in the tech industry, we stay ahead of trends, such as the latest advancements in PHP 8.4, to ensure our clients benefit from state-of-the-art tools and practices. Whether you’re a business seeking optimized web solutions or a developer looking for insights, 200OK Solutions is your trusted partner in growth and innovations.
Visit us at 200OK Solutions to explore how we can elevate your technology strategy