PHP 8.2 in WordPress plugins: readonly classes, DNF types and dynamic properties
PHP 8.2 (December 2022) is now the version many hosts offer by default, and it brings one change that produces warnings in old plugin code and three features that make new code cleaner. The warnings come from dynamic properties; the features are readonly classes, DNF types and the standalone types true, false and null.
Problem: After the switch to PHP 8.2, a plugin fills the log with “Creation of dynamic property … is deprecated”. The same plugin has value objects with long constructors and @var comments that nobody checks.
Solution: Declare every property the class uses (or mark classes that really need dynamic properties with #[\AllowDynamicProperties]), write value objects as readonly class with constructor promotion and “with” methods for changed copies, and use DNF types where a parameter accepts an intersection or null, with Requires PHP: 8.2 in the plugin header.
Dynamic properties are deprecated
class Ha_Widget_Settings {
public function __construct( array $args ) {
$this->title = $args['title'] ?? ''; // PHP 8.2: Deprecated: Creation of dynamic property
}
}
Assigning a property that is not declared still works, but emits a deprecation notice; PHP 9 will turn it into an error. The fix is to declare it:
class Ha_Widget_Settings {
public string $title = '';
public function __construct( array $args ) {
$this->title = (string) ( $args['title'] ?? '' );
}
}
Classes that store arbitrary data on purpose can keep the old behavior with an attribute, which is also what WordPress core did for some of its classes:
#[\AllowDynamicProperties]
class Ha_Legacy_Bag {}
stdClass and classes with __get() / __set() are not affected. A quick way to find the affected classes is to run the plugin’s tests or click through it with WP_DEBUG on and search debug.log for “dynamic property”.
readonly classes
readonly class Ha_Price {
public function __construct(
public int $amount, // in cents
public string $currency,
) {}
public function with_amount( int $amount ): static {
return new static( $amount, $this->currency );
}
public function format(): string {
return sprintf( '%s %s', number_format_i18n( $this->amount / 100, 2 ), $this->currency );
}
}
$price = new Ha_Price( amount: 1999, currency: 'EUR' );
$sale = $price->with_amount( 1499 );
// $price->amount = 1; Error: Cannot modify readonly property Ha_Price::$amount
readonly on the class makes every declared property readonly (PHP 8.1 had it only per property) and forbids dynamic properties. All properties must have a type. Since the object cannot change, a modified copy is a new object; a with_*() method makes that explicit. Readonly properties and enums from PHP 8.1: PHP 8.1 enums, readonly properties and intersection types.
DNF types and true, false, null
function ha_count_items( ( Countable&Traversable )|array|null $items ): int {
if ( null === $items ) {
return 0;
}
return is_array( $items ) ? count( $items ) : iterator_count( $items );
}
function ha_is_enabled(): bool {
return (bool) get_option( 'ha_enabled' );
}
/** Returns the post or false, like many core functions. */
function ha_find_post( int $id ): WP_Post|false {
return get_post( $id ) ?? false;
}
DNF (disjunctive normal form) allows unions of intersections: here an object that is both Countable and Traversable, or an array, or null. PHP 8.1 allowed intersections only alone. false was allowed in unions since PHP 8.0; since 8.2, true, false and null can also stand alone. They cannot be combined into true|false, which is what bool is for. WP_Post|false describes the many WordPress functions that return false on failure; get_post() itself returns null, hence the ?? false.
Hide secrets in stack traces
function ha_api_login( string $user, #[\SensitiveParameter] string $password ): bool {
// ...
}
When an exception is thrown inside, the stack trace in the log shows Object(SensitiveParameterValue) instead of the password. Useful for every function that receives API keys or passwords.
Supporting several PHP versions
Code with readonly class or DNF types does not even parse on PHP 8.1: the plugin fails with a parse error before any version check runs. Set Requires PHP: 8.2 in the plugin header, then WordPress refuses to activate it on older versions with a clear message. For plugins that must still support PHP 7.4, keep these features out and fix only the dynamic properties, which is compatible with every version.
NOTE: WordPress core itself is compatible with PHP 8.2 (with “beta support” for 8.2 and newer in the WordPress handbook as of late 2024), but the version of PHP a site can use is decided by its least compatible plugin. Test the whole site on 8.2 in staging before switching production. PHP 8.4 was released in November 2024 with property hooks and asymmetric visibility; adopting it in plugins makes sense once hosts offer it widely.