Sure, here’s the translation:
A Review of the Recent Evolution of PHP
PHP has established itself as the go-to language for backend web development, powering millions of websites and applications worldwide. Each new version brings significant improvements in performance, syntax, and security. From PHP 8.0—which marked a revolution with the JIT engine, union types, and attributes—to PHP 8.4, the evolution has been consistent, aimed at making developers’ lives easier, increasing security, and maintaining the ecosystem’s relevance.
Recent Major Milestones:
- PHP 8.0 (2020): JIT engine, attributes, union types, match improvements, throw as an expression.
- PHP 8.1 (2021): Enumerations (enum), readonly properties, Fibers for concurrency, performance improvements.
- PHP 8.2 (2022): Readonly types, readonly constants, deprecation of dynamic properties, true types.
- PHP 8.3 (2023): Types for class constants, improvements in inheritance, new array functions, optimized performance.
- PHP 8.4 (2024): The focus is on further facilitating clean code, semantic clarity, and security, with new capabilities for working with properties, visibility, data handling, and the DOM API.
What’s New in PHP 8.4?
Property Hooks: Elegant and Native Getters and Setters
One of the most notable advancements in PHP 8.4 is the introduction of property hooks. It is now possible to define custom logic when reading or writing a property directly in the declaration, without needing to manually implement the classic getX
or setX
methods. This enhances readability, reduces errors, and facilitates integration with IDEs and analysis tools.
Example before (PHP <= 8.3):
class Locale {
private string $countryCode;
public function setCountryCode(string $countryCode): void {
$this->countryCode = strtoupper($countryCode);
}
// ...getter...
}
Example with PHP 8.4:
class Locale {
public string $countryCode {
set (string $countryCode) {
$this->countryCode = strtoupper($countryCode);
}
}
}
This also allows the implementation of computed properties and enables safer value assignment.
Asymmetrical Visibility: Granular Control of Read and Write
PHP 8.4 introduces the ability to define different scopes for reading and writing on a public property. This means a property can be public for reading but private for writing, eliminating the need for repetitive methods and enhancing encapsulation.
class Version {
public private(set) string $version = '8.4';
}
Now, any part of the code can read $version
, but only internal methods can modify it.
New DOM API and Full HTML5 Support
The new DOM API based on the Dom
namespace and the HTMLDocument
class allows working with HTML5 documents more reliably and in a standardized way, correcting old errors and improving interoperability.
$dom = Dom\HTMLDocument::createFromString('Example ');
$node = $dom->querySelector('main > article:last-child');
Deprecations and Native Marking of Obsolete Methods
It is now possible to use the #[\Deprecated]
attribute to indicate methods, functions, and class constants that are considered obsolete, generating automatic warnings and aiding in the transition of legacy code.
class MyClass {
#[\Deprecated(message: "Use getVersion instead", since: "8.4")]
public function getOldVersion() { /* ... */ }
}
Improvements in BCMath and Arbitrary Precision Handling
PHP 8.4 introduces the BcMath\Number
class, allowing high-precision mathematical operations with a more intuitive and object-oriented syntax.
use BcMath\Number;
$num1 = new Number('0.12345');
$num2 = new Number('2');
$result = $num1 + $num2; // '2.12345'
New Array Functions
Utilities such as array_find
, array_find_key
, array_any
, and array_all
have been added, bringing PHP’s array handling closer to what modern languages offer.
$animal = array_find(['dog', 'cat', 'cow'], fn($v) => str_starts_with($v, 'g')); // 'cat'
More Notable Changes
- Specific sub-classes for PDO: Better typing and clearer methods for each database.
- Direct access to methods after instantiating (
new MyClass()->method()
without additional parentheses). - More efficient new JIT implementation.
- New multibyte string manipulation functions (mb_trim, mb_ucfirst, etc.).
- Lazy objects API, improvements in DateTime, Reflection, and more.
Quick Comparison: PHP 8.4 vs Previous Versions
Feature | PHP 8.0 | PHP 8.1 | PHP 8.2 | PHP 8.3 | PHP 8.4 |
---|---|---|---|---|---|
JIT Engine | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ Improved |
Enum | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
Readonly Properties | ❌ | ✔️ | ✔️ | ✔️ | ✔️ |
Property Hooks | ❌ | ❌ | ❌ | ❌ | ✔️ New |
Asymmetrical Visibility | ❌ | ❌ | ❌ | ❌ | ✔️ New |
HTML5 DOM API | ❌ | ❌ | ❌ | ❌ | ✔️ New |
#[\Deprecated] Attribute | ❌ | ❌ | ❌ | ❌ | ✔️ New |
New array_functions() | ❌ | ❌ | ❌ | ✔️ | ✔️ Improved |
Object-oriented BCMath | ❌ | ❌ | ❌ | ❌ | ✔️ New |
Backward Compatibility | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ (with deprecations) |
Why Upgrade to PHP 8.4?
- Cleaner and maintainable code thanks to hooks and asymmetrical visibility.
- Increased security with access controls and new auditing functions.
- Better performance due to JIT optimizations and new native functions.
- Greater compatibility and support for modern standards in HTML5, arrays, and numerical precision.
- Ease for developers with less boilerplate, clearer error messages, and automatic deprecation warnings.
Conclusion
PHP 8.4 solidifies the maturity of the language, making it easier to write cleaner, more robust, and secure code. The upgrade is particularly attractive for both new and existing projects looking to leverage the latest trends in web development, enhance their performance, and facilitate the evolution of their codebase.
The recommendation for technical teams and developers is clear: if your stack allows it, make the jump to PHP 8.4 and take advantage of its new architecture and advanced functionalities.