An Overview of What’s Coming in PHP 8.4
Author : Rabia Akram
Upcoming Features in PHP 8.4
PHP 8.4 is on the horizon, and in this post, we'll explore what's been announced so far and what new features you can look forward to.
When Will PHP 8.4 Be Released?
PHP 8.4 is scheduled for release on November 21, 2024. The release will be preceded by six months of pre-release phases, progressing from Alpha versions to Beta versions, then Release Candidates, and finally the official release.
New Array Find Functions
PHP 8.4 introduces new array find functions, including:
array_find()array_find_key()array_any()array_all()
For more details, see our post on PHP 8.4 Array Find Functions.
PHP Property Hooks
Inspired by languages like Kotlin, C#, and Swift, PHP 8.4 will introduce property hooks with syntax resembling short and multi-line closures:
phpclass User implements Named {
private bool $isModified = false;
public function __construct(
private string $first,
private string $last
) {}
public string $fullName {
get => $this->first . " " . $this->last;
set {
[$this->first, $this->last] = explode(' ', $value, 2);
$this->isModified = true;
}
}
}
These hooks will streamline property getters and setters, allowing properties to define custom access and update logic.
For more details, check out our post: Property Hooks in PHP 8.4.
Simplified Class Instantiation Syntax
Currently, accessing class members during instantiation requires parentheses, but PHP 8.4 will allow you to access methods and properties without the need for wrapping parentheses:
php// Required in PHP <= 8.3
$request = (new Request())->withMethod('GET')->withUri('/hello-world');
// New syntax in PHP 8.4
$request = new Request()->withMethod('GET')->withUri('/hello-world');
This change simplifies code and aligns PHP more closely with other C-style languages like Java, C#, and TypeScript.
Read more in our post: Class Instantiation Without Extra Parentheses in PHP 8.4.
New DateTime Creation from Unix Timestamp
PHP 8.4 introduces a createFromTimestamp() method to create DateTime objects directly from Unix timestamps, including those with microseconds:
php$dt = DateTimeImmutable::createFromTimestamp(1718337072);
$dt->format('Y-m-d'); // 2024-06-14
$dt = DateTimeImmutable::createFromTimestamp(1718337072.432);
$dt->format('Y-m-d h:i:s.u'); // 2024-06-14 03:51:12.432000
This new method simplifies the process of creating DateTime instances from timestamps, compared to previous versions.
New Multibyte String Functions
PHP 8.4 adds multi-byte support to commonly used string functions:
mb_trimmb_ltrimmb_rtrimmb_ucfirstmb_lcfirst
These functions offer the same functionality as their original counterparts but with support for multi-byte character encoding.
These enhancements and new features in PHP 8.4 are designed to simplify development and bring PHP more in line with modern programming practices.
People Reviews
Based on 0 reviews