
PHP 8.4 Preview: Những thay đổi sắp tới trong PHP
PHP 8.4 dự kiến phát hành vào tháng 11/2024 với nhiều tính năng mới thú vị. Bài viết này preview những thay đổi quan trọng nhất đang được phát triển.
Nội dung chính
Property Hooks (RFC Approved)
Tính năng được chờ đợi nhất! Property hooks cho phép định nghĩa getter/setter trực tiếp trong property declaration:
class User {
public string $fullName {
get => $this->firstName . ' ' . $this->lastName;
set => [$this->firstName, $this->lastName] = explode(' ', $value, 2);
}
public string $email {
set {
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
throw new InvalidArgumentException('Invalid email');
}
$this->email = strtolower($value);
}
}
}
$user = new User();
$user->fullName = "John Doe";
echo $user->firstName; // JohnKhông còn cần magic methods __get/__set hay các getter/setter methods!
Asymmetric Visibility
Có thể set visibility khác nhau cho read và write:
class BankAccount {
// Public read, private write
public private(set) float $balance = 0;
public function deposit(float $amount): void {
$this->balance += $amount;
}
}
$account = new BankAccount();
echo $account->balance; // OK - public read
$account->balance = 100; // Error! - private writenew MyClass()->method() Syntax
Syntax mới cho phép chain method ngay sau new:
// PHP 8.3
$result = (new MyClass())->process()->getResult();
// PHP 8.4 - Không cần parentheses!
$result = new MyClass()->process()->getResult();array_find() và array_find_key()
Functions mới để tìm kiếm trong array:
$users = [
['name' => 'Alice', 'age' => 25],
['name' => 'Bob', 'age' => 30],
['name' => 'Charlie', 'age' => 35],
];
// Tìm user đầu tiên >= 30 tuổi
$user = array_find($users, fn($u) => $u['age'] >= 30);
// ['name' => 'Bob', 'age' => 30]
$key = array_find_key($users, fn($u) => $u['age'] >= 30);
// 1HTML5 Parser
DOM extension được nâng cấp với HTML5 parser:
$dom = Dom\HTMLDocument::createFromString($html);
// Hỗ trợ đầy đủ HTML5
$dom = Dom\HTMLDocument::createFromFile('page.html');
// Các methods mới
$element = $dom->querySelector('.my-class');
$elements = $dom->querySelectorAll('div.container');Lazy Objects
Hỗ trợ lazy initialization cho objects:
class HeavyService {
public function __construct() {
// Heavy initialization
sleep(2);
}
}
// Object được tạo nhưng constructor chưa chạy
$service = ReflectionClass::newLazyGhost(HeavyService::class);
// Constructor chạy khi truy cập property/method đầu tiên
$service->doSomething();Deprecated Features
implode()với separator sau array (deprecated từ 7.4, sẽ remove)- Một số Intl extension methods
- Session functions với invalid session handlers
JIT Improvements
PHP 8.4 tiếp tục cải thiện JIT compiler:
- Better type inference
- Improved function inlining
- Reduced memory usage
Migration Notes
Để chuẩn bị cho PHP 8.4:
# Test với PHP 8.4 RC
docker run -it php:8.4-rc-cli php -v
# Chạy static analysis
./vendor/bin/phpstan analyse -l 9 src/
# Check deprecations
composer require --dev roave/no-leaksFullstack Station Tips
PHP 8.4 mang đến những cải tiến significant:
- Property hooks – Game changer cho class design, tương tự C# properties
- Asymmetric visibility – Immutable objects dễ dàng hơn
- HTML5 parser – Xử lý HTML hiện đại không còn hack
Mình đặc biệt excited về property hooks – sẽ làm code clean hơn rất nhiều. Hãy bắt đầu đọc RFCs và test với RC builds để chuẩn bị upgrade!
