1724837370

What's new in PHP 8.4


PHP 8.4 will be released on November 21, 2024. It'll include property hooks, HTML 5 support, as well as chaining methods on new without additional parentheses — a big one! ## <br>Property hooks One of the biggest changes in modern-PHP history: the ability to define property hooks, eliminating the need for a lot of boilerplate code. ```php 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; } } ``` The goal of property hooks is to remove a lot of getters and setters, by allowing each property to define its own `get` and `set` hooks. Hooks are optional, and you don't have to add both of them on a specific property. For example, a property with only a `get` hook is virtual property. There is a lot to say about property hooks, and I plan to write a followup post on them soon, so make sure to subscribe if you want to know when that one is done. One final thing I'd like to mention — probably what I'm most hyped about: property hooks can be defined in interfaces! ```php interface HasAuthors { public string $credits { get; } public Author $mainAuthor { get; set; } } ``` `new` **without parentheses** As if property hooks alone wasn't enough, PHP 8.4 has another feature that will save so much boilerplate code: you don't have to wrap `new` invocations within parenthesis anymore to be able to chain methods on them. So instead of doing this: ```php $name = (new ReflectionClass($objectOrClass))->getShortName(); ``` You can now do this: ```php $name = new ReflectionClass($objectOrClass)->getShortName(); ``` For more details you can go to this [link](https://stitcher.io/blog/new-in-php-84). Let us know in the comments what you think of the new PHP functions

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]