PHP 8.4: What's New and Why It Matters
Explore the latest features in PHP 8.4 including property hooks, asymmetric visibility, and performance improvements that will change how you write PHP.
PHP 8.4: What's New and Why It Matters
The PHP community is buzzing with excitement about PHP 8.4, and for good reason. This release introduces groundbreaking features that will fundamentally change how we write PHP code.
Property Hooks: A Game Changer
One of the most anticipated features in PHP 8.4 is property hooks. This feature allows you to define getter and setter logic directly on properties without writing explicit methods.
Before PHP 8.4
1class User {
2 private string $email;
3
4 public function getEmail(): string {
5 return $this->email;
6 }
7
8 public function setEmail(string $email): void {
9 if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
10 throw new InvalidArgumentException('Invalid email');
11 }
12 $this->email = strtolower($email);
13 }
14}With PHP 8.4 Property Hooks
1class User {
2 public string $email {
3 set {
4 if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
5 throw new InvalidArgumentException('Invalid email');
6 }
7 $this->email = strtolower($value);
8 }
9 }
10}
11
12$user = new User();
13$user->email = 'JOHN@EXAMPLE.COM';
14echo $user->email; // Outputs: john@example.comThis is huge for code readability and reduces boilerplate significantly!
Asymmetric Visibility
Another powerful feature is asymmetric visibility, allowing different access levels for reading and writing properties:
1class Product {
2 public private(set) string $id;
3 public private(set) float $price;
4
5 public function __construct(string $id, float $price) {
6 $this->id = $id;
7 $this->price = $price;
8 }
9
10 public function applyDiscount(float $percent): void {
11 $this->price *= (1 - $percent / 100);
12 }
13}
14
15$product = new Product('SKU-123', 99.99);
16echo $product->price; // ✅ Works
17$product->price = 79.99; // ❌ ErrorNew Array Functions
PHP 8.4 introduces several new array functions:
1$users = [
2 ['id' => 1, 'name' => 'Alice'],
3 ['id' => 2, 'name' => 'Bob'],
4];
5
6$user = array_find($users, fn($u) => $u['id'] === 2);
7$key = array_find_key($users, fn($u) => $u['name'] === 'Alice');
8$hasAdmin = array_any($users, fn($u) => $u['role'] === 'admin');Performance Improvements
Should You Upgrade?
Absolutely! PHP 8.4 offers better developer experience, improved performance, and more expressive code.
Conclusion
PHP 8.4 represents a major leap forward for the language. Start planning your upgrade today!
Enjoyed this article?
Share it with your network!