PHP 8.3: New Features, Deprecations, and More

Share This Post!

As part of its yearly release cycle, PHP continues the tradition of launching a new version toward the end of each year, with PHP 8.3 being the latest.

Great news for developers and businesses: Symple Logix now supports PHP 8.3 on Symple Logix hosting plans. This version brings enhanced performance, improved security, and smoother functionality for applications.

New Additions to PHP 8.3

1. Typed Class Constants

PHP 8.3 introduces typed class constants, allowing developers to specify types for constants directly in classes, interfaces, traits, and enums. This feature enhances code consistency and prevents unintentional type changes across derived classes, helping maintain type integrity.

interface ConstExample {
    const string VERSION = "PHP 8.3";
}

// Illegal redeclaration
interface ConstExample {
    const float VERSION = "PHP 8.3";
}

Typed constants are especially useful in inheritance, as they ensure that child classes can’t alter the type of inherited constants, reducing the chance of compatibility issues.

2. json_validate() Function

Before PHP 8.3, validating JSON syntax required using json_decode() and error checking. With PHP 8.3, the new json_validate() function simplifies this by directly checking JSON syntax without the need to decode the string into arrays or objects. This saves memory and streamlines the process, particularly useful for validating JSON payloads before storing or sending them in requests.

if (json_validate($maybeJSON)) {
    // Process valid JSON
}

This function helps ensure valid JSON before handling it further.

3. Dynamic Class Constant and Enum Number Fetch Support in PHP 8.3

PHP 8.3 introduces a simpler method for dynamically accessing class constants and enum members. Instead of using the constant() function, developers can now use a more intuitive syntax to fetch class constants and enum members using variable names, improving readability and simplifying code:

$constantName = 'THE_CONST';
$memberName = 'FirstMember';
echo MyClass::{$constantName};
echo MyEnum::{$memberName}->value;

This change makes dynamic access to constants and enum members more straightforward.

4. gc_status() Now Returns Additional Information

In PHP 8.3, the gc_status() function has been enhanced to provide more detailed information about the garbage collector. Developers can now access the status of the garbage collection process, including the running status, memory usage before and after collection, and the protection status.

$gcStatus = gc_status();
echo "Running: " . $gcStatus['Running'];
echo "Memory usage before collection: ".$gcStatus['memoryUsageBefore'];
echo "Memory usage after collection: " .$gcStatus['memoryUsageAfter'];

This update offers a clearer insight into the garbage collection process, aiding in performance tuning.

5. New \Random\Randomizer::getBytesFromString Method

PHP 8.3 introduces the getBytesFromString method in the \Random\Randomizer class. This allows developers to generate random bytes based on a predefined string of characters. It offers greater control over random data generation by selecting characters from a custom set.

$rng = new Random\Randomizer();
$alpha = 'ABCDEFGHJKMNPQRSTVWXYZ';
echo $rng->getBytesFromString($alpha, 6); // Example output: "MBXGWL"

This method provides flexibility in generating secure, random byte sequences tailored to specific character sets.

6. New \Random\Randomizer::getFloat() and nextFloat() Methods

PHP 8.3 adds getFloat() and nextFloat() methods for generating random float values within a specified range. These methods improve precision and control over random floating-point number generation.

$rng = new Random\Randomizer();
echo $rng->getFloat(0, 5); // Example output: 2.3937446906217

These new methods offer developers more options for random number generation within defined limits.

Whether it’s the problem-solving abilities of dolphins, the cunning of corvids, the enigmas of octopuses, or the dexterity of border collies, these animals remind us of nature’s contemplation and insight and its limitless capacity for versatility.

7. Fallback Value Support for PHP INI Environment Variable Syntax

PHP 8.3 introduces support for fallback values in PHP INI settings. This allows developers to define default values when environment variables are missing, improving configurability.

session.name = ${SESSION_NAME:-Foo}
sendmail_from = "${MAIL_FROM_USER:-info}@${MAIL_FROM_DOMAIN:-example.com}"

This feature simplifies configuration management by gracefully handling missing environment variables.

8. PHP CLI Lint Supports Linting Multiple Files at Once

PHP 8.3 enhances the CLI linting process by supporting multiple files in a single command, improving efficiency for developers checking syntax errors across multiple files.

php -l file1.php file2.php file3.php
9. class_alias() Supports Aliasing Built-in PHP Classes

PHP 8.3 expands class_alias() to allow aliasing built-in PHP classes, offering more flexibility in code organization.

class_alias(\DateTime::class, 'MyDateTime');
$customDateTime = new MyDateTime();
10. New stream_context_set_options Function

A new function, stream_context_set_options, replaces stream_context_set_option, allowing developers to set multiple options for stream contexts, offering a more versatile API.

stream_context_set_options($stream_or_context, ['http' => ['method' => 'POST']]);

Deprecations and Changes in PHP 8.3

1. get_class() and get_parent_class() Changes

PHP 8.3 deprecates calling get_class() and get_parent_class() without parameters. Developers should now provide an object to these functions to avoid deprecation notices.

2. unserialize(): E-NOTICE to E-WARNING

PHP 8.3 raises errors for invalid data passed to unserialize() as E_WARNING instead of E_NOTICE, ensuring more consistent error handling.

3. HTML Highlight Tag Changes

PHP 8.3 changes the output of highlight_file and highlight_string to use <pre><code></code></pre>, improving readability and aligning with modern HTML standards.

4. Granular DateTime Exceptions

PHP 8.3 introduces \DateTimeException, improving error handling for date-related issues with more specific exception classes.

Share This Post!