PHP 8.2 Available to All FastComet Customers

PHP 8.2, released for general availability on December 8, 2022, is now fully available for all FastComet clients. We conducted extensive testing and monitoring to ensure that the PHP 8.2 integration with cPanel and our infrastructure worked flawlessly. We can confidently state that the new PHP version, which provides stability and optimal performance for your PHP applications, is safe to deploy.

The new version of the programming language includes several exciting new features. New readonly classes; allow true, false, and null as standalone types; new ways to fetch enums; and many more are among them.

This article will cover most of what’s new, changed, and deprecated in PHP 8.2, from new features and performance improvements to major changes and deprecations.

PHP 8.2 for all FastComet Users

PHP 8.2 is now available on all FastComet shared servers, and all clients can use the FastComet PHP Selector to select it quickly. This is the fastest PHP version yet, so we recommend upgrading if your site is fully compatible. Keep in mind, however, that some plugins may not be compatible with the new version. Remember to thoroughly test your website after upgrading to a new PHP version.

New Features and Improvements in PHP 8.2

Let’s start by going over all of the new PHP 8.2 features. It’s quite a lengthy list:

New readonly Classes

The readonly feature for class properties was introduced in PHP 8.1. PHP 8.2 now includes the ability to declare the entire class as readonly.

When you declare a class as readonly, the readonly feature is automatically inherited by all of its properties. Declaring a class readonly is equivalent to declaring all class properties readonly.

In PHP 8.1, you had to declare each property as read-only:

class WorkClassOne
{
public readonly string $ValueA,
public readonly int $ValueB
}

With PHP 8.2, you can simply write:

readonly class WorkClassOne
{
public string $ValueA,
public int $ValueB
}

You can now mark abstract or final classes as readonly. The order of the keywords is irrelevant here.

abstract readonly class Work {}
final readonly class Home {}

A readonly class with no properties can also be declared. This prevents dynamic properties while allowing child classes to declare their readonly properties explicitly.

Subsequently, classes marked as readonly can only possess properties with specified types and the same restriction applies when declaring individual properties as readonly. If it is not possible to declare a property with a specific type, then the mixed type property can be utilized.

Declaring a readonly class without a typed property will result in a Fatal error:

readonly class Work{
public $task;
}
Fatal error: Readonly property Work::$task must have type in … on line …

Also, you cannot declare the following PHP features as readonly:

  • Enums;
  • Traits;
  • Interfaces.

Declaring these features as readonly will result in a Parse error.

readonly trait LoginMessage {}

Parse error: syntax error, unexpected token "trait," expecting "abstract" or "final" or "readonly" or "class" in ... on line …

Considering the behavior of PHP keywords, the readonly keyword is case insensitive.

Another thing to consider is that while dynamic properties are deprecated in this version of PHP, they can still be added to a class. Still if you do it for a readonly class that will result in a Fatal Error:

Fatal error: Readonly property Work::$task must have type in ... on line ...

Allow true, false, and null as Standalone Types

Scalar types such as int, string, and bool are already available in PHP. That was expanded upon in PHP 8.0 with the addition of union types, allowing for different value types. The exact request also permitted the use of false and null as part of a union type but not as standalone types.

A fatal error occurred if you attempted to declare false or null or as standalone types without them being part of a union type.

PHP 8.2 supports the standalone types false and null to avoid this scenario. PHP’s type system is now more expressive and complete due to this addition. You can now precisely declare the return, parameter, and property types.

Also, up to PHP 8.1, the programming language still lacks a true type, which is a natural counterpart to the false type. 

PHP 8.2 corrects this and adds support for the true type. It does not allow coercion, just as the false type does.

The true and false types both belong to PHP’s bool type. To eliminate duplication, it is not possible to have all three types declared together in a union type. This will cause a compile-time fatal error.

Fetch enum Properties in const Expressions

This RFC proposes the adoption of the ->/?-> operator for accessing enum properties in constant expressions. The purpose of this addition is to eliminate the need for duplicating enum values in cases where enum objects cannot be used, such as in array keys.By enabling the retrieval of enum properties in these restricted areas, code can be made more straightforward.

In addition, there is included support for the null-safe operator ?->.

Disjunctive Normal Form (DNF) Types

The Disjunctive Normal Form (DNF) standardizes the organization of boolean expressions. It is made up of a disjunction of conjunctions — an OR of ANDs in boolean terms.

The use of DNF in type declarations provides a consistent way to write combined Union and Intersection types that the parser can handle. The new DNF types feature in PHP 8.2 is simple but powerful when used correctly.

Allow Constants in Traits

PHP includes a feature called Traits that allows you to reuse code. They’re great for reusing code across classes.

Currently, only methods and properties can be defined using traits; constants are not supported. In other words, a trait’s expected invariants cannot be defined by the trait itself. To get around this restriction, you must define constants in its composing class or an interface that its composing class implements.

This RFC proposes allowing Traits to define constants. These constants can be defined in the same way that class constants are. This example, taken directly from the RFC, clarifies its application:

trait Foo
{
    public const CONSTANT = 1;
}

class Bar
{
    use Foo;
}

var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error

Trait constants, like Trait property and method definitions, are merged into the composing class’s definition. They are also bound by the same rules as trait properties. According to the RFC, while this proposal is a good starting point, more work is needed to flesh out the feature.

Deprecations in PHP 8.2

We can now proceed to the deprecations in PHP 8.2. To be quick, we will provide source links for them:

Minor Changes in PHP 8.2

Just like with the deprecations, here are some source links with the minor changes in PHP 8.2:

Additional RFCs in PHP 8.2

Now with the list of additional RFCs in PHP 8.2:

  1. New ini_parse_quantity function;
  2. New memory_reset_peak_usage function;
  3. Enable /n modifier in preg_* functions;
  4. Make the iterator_*() family accept all iterables.

How to Upgrade to PHP 8.2

With FastComet’s shared hosting servers, you have the convenient option to select your PHP version quickly and easily. All you have to do is:

  • Go to your Client AreacPanelSelect PHP Version:
Find PHP Selector in cPanel Jupiter
  • In the PHP Selector, click on the drop-down:
Choose PHP 8.2 in the PHP Selector
  • Then, click on Apply:
Set PHP 8.2 as the Current PHP Version
  • Done!

When switching between PHP versions, make sure your project is compatible before starting the transition.

Summary

PHP 8.2 builds on the significant advancements made in the last two PHP versions. The most exciting PHP 8.2 features, in our opinion, are the new standalone types and readonly properties which again optimize PHP as a language even further.

Once again, there are many changes and we can’t wait to see what will come with the programming language’s new version.

Use the comment section to share with us which PHP 8.2 changes and improvements you like the most or perhaps what you dislike. Happy Coding!

Joseph

Joseph is part of the FastComet Marketing team. With years of content writing experience behind him, it's one of his favorite activities. Joseph takes part in the SEO of the FastComet website and blog. His goal is to write comprehensive posts and guides, always aiming to help our clients with essential information. Joseph also has a thirst for knowledge and improvement, which makes the hosting environment a perfect place for him.