PHP 8.3 will be updated on November 23, 2023. It will add typed class constants, a new helper function for json_validate to check json payloads, and some minor improvements to the Randomizer class, reading ini configuration, etc.
#The PHP team will release a new minor version of PHP at the end of this month. The new version will mainly contain improvements and features that, except for a few, will not be very relevant for end users.
Typed Class Constants
We all use constants at some point. So far, the type of a constant has been inferred from its value. This also means that the ability to inherit or implement a class can change the type. In a nutshell, the new functionality will look like this:
class Foo { public int BAZ = 1; }
Validate JSON payload
In almost all situations where JSON must be read, transformed, and written , must be verified first. You can do a "test decode" to see if an exception is thrown, or just set up an else branch for invalid payload cases.
For smaller payloads this might be ok, but decoding a very large JSON string into an array just to check if it's valid is not elegant. Additionally, it can cause memory and/or performance issues.
The new json_validate function promises to check whether a given string is valid JSON and is more performant and memory efficient. As shown below:
json_validate(string $json, int $depth = 512, int $flags = 0): bool
Further improvements
As I said, there are further improvements in the upcoming PHP 8.3 version. Since I personally don't think they are important, I just want to mention them here for completeness.
Randomizer Improvements: Small improvements, such as specifying a range for random floating point values or defining the string length.
Read-only revision: Allow read-only properties to be reinitialized on cloning. This seems like an edge case, but may be important for deep cloning.
#[Override] attribute: This attribute is used to express the programmer's "intention". It essentially says "I know this method comes from the parent class and I want to override it. If this changes, please let me know."
Anonymous read-only class: somewhat self-explanatory. Until now, there was no way to create them, now PHP introduces it.
Dynamic class constant acquisition: PHP allows the use of curly braces to read dynamic class properties. With 8.3, it's also possible to read constants (seriously, don't do that).
Static properties in Traits: Static properties inherited from the parent class will be redeclared. Personally, it's an evil mix of everything you can do in PHP. Why? check.
there are more. Since I personally don't think they are relevant, I haven't listed them here. For a complete list of all changes, check out the official migration guide.
Conclusion
As with every new release, there is no golden rule to follow whether to update or not. We strongly recommend using the latest version, as it often fixes bugs and bugs in previous versions and provides better performance and functionality.
The above is the detailed content of A new version of PHP 8.3 is about to be released: an overview of new features. For more information, please follow other related articles on the PHP Chinese website!