Member-only story
Exploring PHP 8.4: What’s New and How to Use It
4 min readJan 6, 2025
With the release of PHP 8.4, we’re seeing some truly exciting additions and improvements. In this article, I’ll walk you through the highlights of PHP 8.4, complete with practical examples to help you make the most of these new features.
1. Property Hooks
One of the standout features in PHP 8.4 is the introduction of property hooks. These hooks allow us to define custom behavior when accessing or modifying class properties. This means you can now add logic to properties without resorting to magic methods like __get
and __set
.
class Product {
private float $price;
public function __get(string $name) {
if ($name === 'price') {
return $this->price;
}
throw new Exception("Property $name does not exist");
}
public function __set(string $name, $value) {
if ($name === 'price') {
if ($value < 0) {
throw new Exception("Price cannot be negative");
}
$this->price = $value;
} else {
throw new Exception("Property $name does not exist");
}
}
}
$product = new Product();
$product->price = 100.50; // Valid
echo $product->price; // Outputs: 100.5