PHP 8.4 Has Arrived! Updates That Will Excite You
Herminio Heredia
1 month ago
Get ready for a pure adrenaline rush! PHP 8.4 is here, packed with new features to make your life easier and your code more powerful. Want to know what’s in store? Keep reading!
In this post, you’ll discover:
- New array functions (say goodbye to endless loops!)
- Property Hooks: Goodbye repetitive code!
- Parenthesis-free class instantiation: Simpler and cleaner!
- And much, much more...
Find What You’re Looking For in Your Arrays with PHP 8.4
Tired of iterating through arrays with loops? PHP 8.4 introduces new functions to make your life easier:
-
array_find()
: Finds the first element that meets a condition. -
array_find_key()
: Gets the key of the first element that meets a condition. -
array_any()
: Checks if at least one element meets a condition. -
array_all()
: Checks if all elements meet a condition.
Say goodbye to foreach
loops and write cleaner, more efficient code!
Property Hooks: The End of Repetitive Code!
Inspired by languages like Kotlin, C#, and Swift, Property Hooks allow you to define custom actions when reading or writing a property. Say goodbye to traditional getters and setters!
class User
{
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;
}
}
}
With Property Hooks, your code becomes more concise and readable.
Parenthesis-Free Class Instantiation: Simpler and Cleaner!
Remember those annoying parentheses when instantiating a class and accessing its members? PHP 8.4 eliminates the need for them!
// Before:
$request = (new Request())->withMethod('GET')->withUri('/hello-world');
// Now:
$request = new Request()->withMethod('GET')->withUri('/hello-world');
A small change that makes a big difference in your code’s readability.
Create a DateTime from a Unix Timestamp
PHP 8.4 makes it easier with the new createFromTimestamp()
method. It supports both standard Unix timestamps and those with microseconds!
$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
New mb_
Functions for Multibyte Strings
PHP 8.4 adds multibyte string support to functions like trim
, ltrim
, rtrim
, ucfirst
, and lcfirst
. Now you can work seamlessly with strings in any language!
-
mb_trim()
-
mb_ltrim()
-
mb_rtrim()
-
mb_ucfirst()
-
mb_lcfirst()
Asymmetric Property Visibility: More Control Over Your Classes!
You can now define different levels of visibility for reading and writing a property, offering more flexibility and security for your classes.
class Book
{
public function __construct(
public private(set) string $title,
public protected(set) string $author,
protected private(set) int $pubYear,
) {}
}
Want to Learn More?
This is just the tip of the iceberg! PHP 8.4 comes with many more updates and improvements. Visit the official PHP 8.4.0 Release Announcement page to learn all the details.
Additional Resources:
- UPGRADING: Discover the new features, breaking changes, and everything you need to know to upgrade to PHP 8.4.
- Downloads: Download the PHP 8.4 source code.
Don’t wait any longer—dive into the exciting world of PHP 8.4 today!