The PHP staff has launched PHP 8.4 at present with new array discover features, property hooks, class instantiation with out additional parenthesis, and extra:
See the PHP 8.4 RFC web page for a whole checklist.
New Array Discover Features
PHP 8.4 will include new array discover features that embody:
-
array_find()
-
array_find_key()
-
array_any()
-
array_all()
See our publish on the PHP 8.4 Array Discover Features.
PHP Property Hooks
Property hooks are impressed by languages like Kotlin, C#, and Swift, and the syntax contains two syntax variants that resemble brief and multi-line closures:
class Person implements Named
{
personal bool $isModified = false;
public operate __construct(
personal string $first,
personal string $final
) {}
public string $fullName {
// Override the "learn" motion with arbitrary logic.
get => $this->first . " " . $this->final;
// Override the "write" motion with arbitrary logic.
set {
[$this->first, $this->last] = explode(' ', $worth, 2);
$this->isModified = true;
}
}
}
Property hooks will assist take away boilerplate of property getters and setters, permitting a property to outline entry and updates utilizing hooks.
Try our publish for extra particulars: Property Hooks in PHP 8.4.
new MyClass()->methodology() with out parentheses
Since member entry throughout instantiation was launched, you need to wrap the new MyClass()
name in parentheses, otherwise you’ll get a parse error. The proposed syntax would help you entry constants, properties, and strategies with out the additional parentheses:
// Wrapping parentheses are required to entry class members
$request = (new Request())->withMethod('GET')->withUri('/hello-world');
// PHP Parse error (<= PHP 8.3): syntax error, sudden token "->"
$request = new Request()->withMethod('GET')->withUri('/hello-world');
This replace fixes papercut that makes working with class member entry less complicated, not having so as to add surrounding parentheses or utilizing a static constructor methodology. This syntax change additionally places PHP extra in alignment with different C languages like Java, C#, and TypeScript, which do not require surrounded parentheses.
Try our publish for extra particulars: Class Instantiation With out Further Parenthesis in PHP 8.4.
Create a DateTime from a Unix Timestamp
Making a DateTime
from a Unix timestamp will likely be extra handy in PHP 8.4, with the brand new createFromTimestamp()
methodology. It should help each a typical Unix timestamp in addition to timestamps containing 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
In earlier variations of PHP, there are a couple of methods to create a DateTime occasion from a timestamp, such because the createFromFormat()
methodology:
$dt = DateTimeImmutable::createFromFormat('U', (string) 1718337072);
// DateTimeImmutable @1718337072 {#7948
// date: 2024-06-14 03:51:12.0 +00:00,
// }
$dt = DateTimeImmutable::createFromFormat('U.u', (string) 1718337072.432);
// DateTimeImmutable @1718337072 {#7950
// date: 2024-06-14 03:51:12.432 +00:00,
// }
New mb_ features
PHP has had trim, ltrim, rtrim, ucfirst, and lcfirst features for a very long time, and now in PHP 8.4 it provides mb_
, multi-byte strings help for these features.
-
mb_trim()
-
mb_ltrim()
-
mb_rtrim()
-
mb_ucfirst()
-
mb_lcfirst()
These all carry the identical arguments as the unique features. These features are from two separate RFCs: rfc:mb_trim and rfc:mb_ucfirst.
Uneven Property Visibility
Beginning in PHP 8.4, properties may additionally have their visibility set asymmetrically with a distinct scope for studying and writing. Here is an instance from the documentation:
class Guide
{
public operate __construct(
public personal(set) string $title,
public protected(set) string $writer,
protected personal(set) int $pubYear,
) {}
}
class SpecialBook extends Guide
{
public operate replace(string $writer, int $12 months): void
{
$this->writer = $writer; // OK
$this->pubYear = $12 months; // Deadly Error
}
}
$b = new Guide('Learn how to PHP', 'Peter H. Peterson', 2024);
echo $b->title; // Learn how to PHP
echo $b->writer; // Peter H. Peterson
echo $b->pubYear; // Deadly Error
Try our publish for extra particulars: Uneven Property Visibility in PHP 8.4.
Laravel Herd already helps 8.4
In case you are on the lookout for a simple solution to begin utilizing PHP 8.4, Laravel Herd already contains help and you may change to it tremendous simple.
PHP 8.4.1 is right here! 🎉
One-click to discover all new options in Laravel Herd
— Marcel Pociot (@marcelpociot.bsky.social) November 21, 2024 at 5:16 AM
Be taught extra
To stand up to hurry on these new options, take a look at the PHP 8.4.0 Launch Announcement web page for examples earlier than/after PHP 8.4. Make sure you take a look at the deprecations and backward compatibility breaks.
The next checklist of extra sources has all the pieces it’s essential be taught extra about this launch:
- The UPGRADING doc contains backward-incompatible modifications, new options, and all the pieces else you will have to familiarize your self with modifications in PHP 8.4
- The NEWS doc has detailed notes about every launch
- The PHP 8.4 preparation duties web page has the discharge timeline of the upcoming releases
- You’ll be able to obtain PHP 8.4 supply from the downloads web page