PHP 8.4
In a week or so, PHP 8.4 will be out, and the most notable thing about it, to me at least, is the ability to use property hooks. The abuse of getter/setter methods has always bothered me in OO languages (especially Java), and I'm glad that the getters and setters can be written directly to the property itself now.
Clean Code Evangelists are probably not happy that this encourages directly using the properties instead of a wrapper method, but they can all get fucked anyway. To me, pragmatic and simple to understand code is the best, and religious bullshit cargo-culting of design patterns is not something I'm interested in.
Anyway, this is what it looks like:
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;
}
}
(I stole this from Brent Roose's excellent blog post with a lot more info on PHP 8.4).