Unravelling the Latest Features of PHP 8.3 – Release Announcement!

By: Skynet Technologies USA LLC
8 mins
500
PHP 8.3 released

PHP 8.3 was released on November 23, 2023. This latest PHP version has the latest additions, features, and several crucial improvements. Though officially it is announced as a minor version, however, it has a few changes that will impact your experience with PHP. The latest version is speculated as a version with fewer bugs and faster performance.

So, let’s dive deeper into the release documents and understand how latest PHP version (PHP 8.3) is different from its previous version (PHP 8.2).

PHP 8.3 New Features and Benefits

The latest PHP 8.3 has shipped along with some latest features, but rest assured, it has fewer features than PHP 8.2. Its key features that you can see for sure are:

  • Typed Class Constants

    Since PHP 7.4, we have been able to declare types for class properties. Before PHP 8.3, constants could not declare their type and they were inferred based on their value. But now with this latest version, you can declare constants explicitly. Also, PHP 8.3 has ramifications for interfaces, child classes, and abstract classes, as you are allowed to enforce the type through inheritance, preventing extensions from altering the type.

  • Dynamic class constants and Enum members fetch support

    The following were invalid in previous versions of PHP:

    $constantName = 'SOME_FLAG';
    
    echo SomeClassDefiningTheConstant::{$constantName}; 
    echo SomeEnumDefiningTheMemberName::{$constantName}->value;

    They were only accessible by using the constant ()function:

    echo constant("SomeClassDefiningTheConstant::{$constantName}"); 
    echo constant("SomeEnumDefiningTheMemberName::${constantName}")->value;

    This feature is now extended to class constants and Enum members as well because the properties can be accessed dynamically.

  • json_validate() function

    Previously, to validate a JSON string, it was needed to pass to json_decode() and check if errors were released and/or exceptions were thrown or not (though it was dependent on what flags you provided to the function). Also, this approach to validating large JSON structures was mostly draining off the system memory to determine if it is valid. Not only this, but sometimes, it was one of the reasons to hit PHP’s memory limit to process the structure (in the previous PHP version). While the latest PHP has a function that is more performant and less prone to error.

    The function is written like this:

    function json_validate(string $json, int $depth = 512, int $flags = 0): bool

    Where the $flags argument matches the behaviour of the json_decode() function, without the ability to raise an exception (cases where an exception would be raised will result in a Boolean false return value).

  • Random extension additions

    To make your system more flexible for operations that require random bytes, the “Random” extension was added in PHP 8.2. It was specifically for those requiring cryptographically secure pseudo-random number generation (CSPRNG).

    While PHP 8.3 has many latest methods such as,

    1. Random\Randomizer::getBytesFromString(string $string, int $length): string returns a random number sequence of a specified length that only contains bytes from a specified string. It can be useful for producing things such as random short URLs.

    2. Random\Randomizer::getFloat(float $min, float $max, Random\IntervalBoundary $boundary = Random\IntervalBoundary::ClosedOpen): float and Random\Randomizer::nextFloat(): float can be used to produce a random float value. The value will be produced between $min and $max in the case of getFloat(), either inclusively or exclusively based on the $boundary value (IntervalBoundary is a new Enum defining the various boundary conditions you can use). nextFloat() produces always between 0 and 1 and is like JavaScript’s Math.random() function.

  • Addition of mb_str_pad()

    PHP always had a str_pad() function to start the padding, end it, or for both sides of a string, until it is used to reach a request length. However, the functionality had its limitations in that it could only work for single-byte character encodings to eliminate usage with UTF-8 and other multi-byte encodings.

    Now, PHP 8.3 has a new function, mb_str_pad(string $string, int $length, string $pad_string = “ “, int $pad_type = STR_PAD_RIGHT,?string $encoding = null): string.

    This latest change is to complement the functionality of str_pad(), but with a few differences:

    Multi-byte strings can be used for the string to pad as well as the string representing the padding.

    These will use the encoding of the string to pad by default, but you can specify a particular encoding also.

  • Addition of #[\Override] attribute

    There is one more addition in PHP 8.3, which is the attribute #[\Override]. The attribute can be added to methods to demonstrate that the method is intended to override a parent method. So, the PHP engine will now check to ensure the method exists in the parent or an interface is implemented (with the same syntax) and raise a compile-time error.

    The feature is a boon for PHP developers since it helps them to prevent mistakes while they upgrade code.

YOU MAY ALSO LIKE: Website migration SEO checklist

PHP 8.3 changes and deprecations

Along with many noticeable additions, PHP 8.3 has also come with some deprecations and changes such as:

  • class_alias now supports aliasing PHP built-ins
  • Fallback value support for php.ini environment variable syntax
  • Lint more than one file at a time
  • unserialize() now emits E_WARNING instead of E_NOTICE
  • gc_status() extended information
  • Readonly behavior during cloning
  • Use exceptions by default in the SQLite3 extension
  • More appropriate Date/Time exceptions
  • Assertion behavior deprecations
  • Allow dynamic static value initialization
  • Improve semantics of range () arguments
  • Increment/Decrement operator improvements
  • Improve behavior of array_sum () and array_product () when provided unusable values

Update or migrate your existing website to PHP 8.3

To upgrade your existing website to PHP 8.3, start by checking compatibility with the new PHP version and back up your site. Update all dependencies, including third-party libraries and plugins, to versions compatible with PHP 8.3. Review and modify your codebase to address any deprecated features or functions, and thoroughly test your website in a development environment. Ensure that your database and queries are compatible, optimize performance, and address any security considerations. Once testing is successful, deploy the updated website to your production environment, monitor for issues, and stay informed about future PHP updates for ongoing maintenance and improvements.

Read the documentation and begin the migration or upgrade.

Wrapping up

Apparently, migrating or updating your website or app to PHP 8.3 will provide the benefit of three years of community support. Before you start the process, don’t forget to investigate all the changes thoroughly since they will impact website or app performance eventually.

As a leading PHP development company, we deliver expert PHP solutions whether it’s a simple web solution, complex enterprise-level application, ecommerce solution, CMS site, upgradation, migration, maintenance, and more. Skynet Technologies is widely known for providing robust and scalable PHP solutions to fit your specific business needs while meeting the highest coding standards and guidelines. Get in touch with us via email at [email protected] or request a quote.

183