Today, the PHP team unveiled PHP 8.3, introducing features such as typed class constants, a json_validate() function, dynamic retrieval of a class constant, the #[Override] attribute, and several other enhancements.
- Typed Class Constants
- json_validate() function
- Dynamic class constant fetch
- Marking overridden methods
- mb_str_pad() function
- Fallback value syntax for INI ENV variables
- Support linting multiple files at once
- More Appropriate Date/Time Exceptions
- Deep-cloning of readonly properties
- Randomizer Additions
- and more…
Typed Class Constants
In PHP 8.2, the ability to declare types for constants (const) was still unavailable, potentially causing confusion or ambiguity regarding the type being handled:
Here is an example illustrating how typed constants appear in PHP 8.3:
The json_validate() function
To validate JSON in PHP, you can achieve this by either setting the JSON_THROW_ON_ERROR flag, utilizing json_last_error, or simply checking for null in a json_decode() call:
Starting from PHP 8.3, you also have the option to validate JSON using the json_validate() function:
Dynamic class constant fetch
In PHP version 8.2 and later, dynamically retrieving the value of a class constant was only feasible using the constant() function. The following code would lead to a syntax error:
Starting from PHP 8.3, it is now possible to dynamically access constants from a class.
Fallback value for environment variables in INI files
One notable enhancement in PHP 8.3 is the ability to set default values when using environment variables to define INI settings. This feature streamlines the specification of Docker defaults, explicitly eliminating the need to declare defaults as ENV blocks in a Dockerfile. This can simplify various use cases.
For instance, let’s consider a scenario where you want to enable the DRUPAL_FPM_PORT ENV variable to configure an INI value for the www FPM pool:
Previously, the DRUPAL_FPM_PORT had to be explicitly defined without providing a default value. Now, with PHP 8.3, you can employ a syntax that is reminiscent of a bash/shell script, allowing you to do the following:
Consider including an xdebug.ini file as part of your Dockerfile for development, featuring sensible defaults. This setup allows developers the flexibility to override values according to their preferences.
Fore more documentation and features study visit PHP official documentation