Home Home

3 New Features in PHP 8.1

The Read Only Property

The Read Only property modifier can be used to protect the property from being changed outside of its class. After a read only property has been initialized, It can’t be modified. It will always return the same value, no matter what code runs. Read only properties can be used to model value and data-transfer objects.

Pure Intersection Types

With intersection types a value can satisfy multiple constraints. This change makes for shorter code because each constraint does not need to be declared separately.

The Never Return Type

The Never return type can be used when a function is not supposed to return anything. The PHP documentation defines it as the subtype of every other type. This means that it can be used to replace any other type at inheritance. A good use for it is to redirect types that always call exit.

Summary

There are many other new features in PHP 8.1. These are just a few that appear to have basic functions that I can use as a beginner in PHP.

References

https://www.php.net/releases/8.1/en.php