All New Exciting Features and Enhancements of PHP 8.3
In this article, we will delve into the noteworthy additions and changes introduced in PHP 8.3, shedding light on the key aspects developers need to be aware of.
PHP 8.3: New and Exciting Features
- Typed Class Constants
- stream_context_set_options Function
- Randomizer::getBytesFromString Method
- Fallback Value Support for PHP INI Environment Variable Syntax
- class_alias() Supports Aliasing Built-in PHP Classes
- Dynamic Class Constant and Enum Number Fetch Support
- Randomizer::getFloat() and nextFloat() Methods
- json_validate() Function
- gc_status() Now Returns Additional Information
- PHP CLI Lint Supports Linting Multiple Files at Once
1) Typed Class Constants
In PHP 8.3, a noteworthy improvement comes in the form of typed class constants. This new feature allows developers to specify types for class constants, promoting type coherence and minimizing the chances of inadvertent deviations from the original declaration. This enhancement applies not only to class constants but also extends to constants within interfaces, traits, and enums.
The introduction of typed class constants is particularly beneficial for preserving type uniformity when dealing with subclasses derived from base declarations. This helps prevent unintentional modifications to the type, thereby reducing the risk of compatibility issues.
interface ConstTest {
// Illegal:
const string VERSION = "PHP 8.3";
}
interface ConstTest {
const float VERSION = “PHP 8.3”;
}
2) stream_context_set_options Function
The release of PHP 8.3 introduces an enhanced function called stream_context_set_options, which improves upon the existing stream_context_set_option. This new function is designed to handle multiple options and is anticipated to supplant the original function in upcoming PHP versions.
php
stream_context_set_options($stream_or_context, [‘http’ => [‘method’ => ‘POST’]]);
This update enhances stream context manipulation by providing a more versatile and future-proof API.
3) Randomizer::getBytesFromString Method
In PHP 8.3, the \Random\Randomizer class introduces the getBytesFromString method, which facilitates the creation of random sequences derived from a designated character string. This functionality is particularly valuable for generating secure random bytes from a predetermined set of characters. The inclusion of this method enhances the versatility of random data generation, empowering developers to define the origin of characters for the selection of random bytes.
php
$rng = new Random\Randomizer();
$alpha = ‘ABCDEFGHJKMNPQRSTVWXYZ’;
$rng->getBytesFromString($alpha, 6); // “MBXGWL”
$rng->getBytesFromString($alpha, 6); // “LESPMG”
$rng->getBytesFromString($alpha, 6); // “NVHWXC”
4) Fallback Value Support for PHP INI Environment Variable Syntax
With the introduction of version 8.3, PHP developers now can define default values for PHP INI settings in cases where specific environment variables are not present. This enhancement enhances configurability by offering default values, streamlining configuration management, and facilitating the smooth handling of absent environment variables.
php
// Fallback to ‘Foo’ if SESSION_NAME is not set
session.name = ${SESSION_NAME:-Foo}
// Fallback to ‘info@example.com’ if MAIL_FROM_USER or MAIL_FROM_DOMAIN is not set
sendmail_from = “${MAIL_FROM_USER:-info}@${MAIL_FROM_DOMAIN:-example.com}”
5) class_alias() Supports Aliasing Built-in PHP Classes
With the introduction of PHP 8.3, the class_alias() function now supports the aliasing of built-in PHP classes. This enhancement empowers developers to utilize class_alias() for establishing alternative names for fundamental PHP classes, offering increased versatility in code structuring and naming conventions. Consequently, this improvement facilitates the creation of more organized and expressive code when dealing with native PHP classes.
php
class_alias(\DateTime::class, ‘MyDateTime’);
$customDateTime = new MyDateTime();
6) Dynamic Class Constant and Enum Number Fetch Support
PHP 8.3 introduces a simplified approach to retrieve class constants and enum members using variable names. The previously cumbersome constant() function usage is replaced with a more intuitive syntax, which enhances the readability of code. This enhancement streamlines the dynamic access of class constants and enum members.
php
$constantName = ‘THE_CONST’;
$memberName = ‘FirstMember’;
echo MyClass::{$constantName};
echo MyEnum::{$memberName}->value;
Read the complete post here: https://www.aceinfoway.com/blog/whats-new-in-php-83