PHP 8.3 Is Now Available on ServerPilot

November 13, 2023

We’re excited to announce that PHP 8.3 is now available on servers managed by ServerPilot. There are many improvements, changes, new features, and deprecations in PHP 8.3.

PHP 8.3.0 was released for General Availability (GA) on November 23, 2023. All servers will be automatically updated from the Release Candidate (RC) to the GA release.

Here we'll provide a summary of the most important changes in PHP 8.3. For the full list of changes, see PHP's official documentation on migrating code from PHP 8.2 to 8.3.

PHP 8.3 is available on servers running Ubuntu 18.04 and newer.

Changes in PHP 8.3

New Features in PHP 8.3

Readonly anonymous classes

Anonymous classes may now be marked as readonly. Learn more.

Typed class constants

Class, interface, trait, and enum constants now support type declarations. Learn more.

Using final with trait methods

The final modifier may now be used when using a method from a trait.

The #[\Override] attribute

The new #[\Override] attribute checks that a method exists in a parent class or implemented interface. Learn more.

Dynamic fetching of class constants

Class constants can now be accessed dynamically using the C::{$name} syntax. Learn more.

More flexible static variable initializers

Static variable initializers can now contain arbitrary expressions. Learn more.

Fallback values for environment variables in .ini files

PHP already allows you to reference environment variables when reading .ini files with parse_ini_file(). In PHP 8.3, you can also specify a default/fallback value for an environment variable in case the environment variable is not defined.

For example, in a .ini file that references an environment variable named APP_IMAGE_DIR:

listen = localhost:${APP_IMAGE_DIR}

You can now change the .ini file to provide a default value in case the variable is not defined in the environment:

listen = localhost:${APP_IMAGE_DIR:static/images}

New Function json_validate() in PHP 8.3

The new json_validate() function in PHP 8.3 will be useful to many developers.

The signature of the new function is:

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

The example below shows how json_validate() can be used to check if a string contains malformed JSON before you call json_decode().

<?php
$json_str = '{ test: "" ';  // Missing a closing brace.

if ( json_validate($json_str) === false ) {
  die( "Invalid JSON: " . json_last_error_msg() );
}
$data = json_decode($json_str);

Even if your code is already using json_decode() with the JSON_THROW_ON_ERROR flag to detect invalid JSON, using the new json_validate() function can provide speed and memory improvements when you're working with potentially large JSON strings such as JSON strings from user input.

Details of the validation errors can be retrieved using the functions json_last_error() and json_last_error_msg().

Official PHP documentation for the json_validate() function will be available before the General Availability release of PHP 8.3.0.

Backward-Incompatible Changes in PHP 8.3

Changes in PHP 8.3 that are not backward-compatible will prevent code written for earlier PHP versions from working in PHP 8.3

In addition to the changes listed here, there are also bug fixes in PHP 8.3 that result in backward-incompatibility. These types of backward incompatibilities are often cases where problematic PHP code worked in previous PHP versions but shouldn't have worked the way it did or shouldn't have worked at all because the code was ambiguous.

Traits with static properties now create a separate property

Using traits with static properties will now redeclare the static properties inherited from the parent class. This is analogous to adding the static property to the class directly without traits.

Date/Time's object-oriented methods now use exceptions

For users of the object-oriented interface for the Date/Time extension (e.g. the DateTime::add() method), PHP 8.3 reclassified warnings and errors to instead be exceptions that are specific to the Date/Time extension. The new exceptions are under the DateError and DateException hierarchies.

Procedural-style usage of the Date/Time extension (e.g. the date_add() function) has not changed and will continue to use warnings and errors as it currently does.

Learn more about the Date/Time exception changes.

Deprecations in PHP 8.3

  • Using the increment (++) or decrement (--) operators on empty or non-numeric strings is deprecated. To increment non-numeric strings, use str_increment().
  • Calling get_class() and get_parent_class() without arguments is deprecated.
  • The internationalization constant NumberFormatter::TYPE_CURRENCY is deprecated.
  • Calling ReflectionProperty::setValue() with only one parameter is deprecated.
  • The assert_options() function and various ASSERT_* constants are deprecated.
  • Calling SQLite3::enableExceptions(false) is deprecated.

How to Switch to PHP 8.3

You can change an app to use PHP 8.3 through your app's Settings in ServerPilot. If your app does not work with PHP 8.3, you can easily change back to the version you were using before.

As always, please contact us if you have any questions.