On November 23rd, 2022, the latest iteration of the popular PHP programming language – version 8.3 – arrived, bringing notable enhancements since 8.2. While an incremental update, PHP 8.3 contains meaningful improvements that impact day-to-day development.
This release aims to boost programmers’ productivity, security, and experience through optimizations spanning syntax, typing, functions, and tooling. Despite being a point update, developers can leverage several upgrades to accelerate workflows, write safer code, and streamline debugging.
By staying current with the latest PHP language version, developers can tap into the most up-to-date security, language design, and tooling improvements essential for robust, high-velocity application development in today’s business landscape.
Let’s delve into the latest features introduced in this release:
New Override Attribute
In PHP, interfaces define method signatures that classes implementing those interfaces must provide implementations for. Likewise, subclass inheritance allows overriding parent class methods by redefining them in child classes.
However, bugs can occur if developers inadvertently create entirely new methods instead of truly overriding them due to typos or outdated method names in parent classes. Consequently, this breaks intended polymorphic behavior.
To address this, PHP 8.3 introduces the “#[\Override]” attribute that explicitly signals the language a child class method is meant to override a parent method. By requiring this override tag, PHP will validate that the superclass contains a method with the same name, ensuring overrides are intentional and valid.
This attribute brings strictness to method overriding for greater clarity, encapsulation integrity, and the avoidance of slippery bugs. With override validation, developers can refactor parent classes with confidence, while subclasses rely on superclass interfaces as intended.
getFloat() and nextFloat() Functions
Focusing on the random extension, PHP has released two new functions for generating random numbers: getFloat() and nextFloat().
While the getFloat() method can also accept a third parameter, the nextFloat() method is practically the same as the former, with the restriction of generating random values only in the range of 0 to 1.
Typed Class Constants
PHP 8.3 introduces support for type declarations when defining class constants. This enforces that constants set in child classes match the expected type specified in the parent. Prior PHP versions allowed overriding inherited constants loosely without type checks, frequently causing bugs.
For example, a parent class may define a numeric constant that child classes unintentionally override with a string. By adding types like int, float, string, etc., this inconsistency can be automatically detected and prevented at compile time.
Just like previous releases of PHP, a handful of deprecations have been introduced in PHP 8.3. Here is the final list of deprecations announced by PHP:
- get_class() and get_parent_class() without arguments can no longer be used
- active, assert.bail, assert.callback, assert.exception, and assert.warning have all been deprecated
- U_MULTIPLE_DECIMAL_SEPERATORS constant will now be considered deprecated