PHP 8.4 Features for WordPress Plugin Development

PHP 8.4 for WordPress plugin developers

PHP 8.4 (November 2024) has two features that change how classes are written, property hooks and asymmetric visibility, a set of small helpers like array_find(), and one deprecation that affects a lot of existing plugin code: implicitly nullable parameter types. A plugin that should run without notices on hosts that move to 8.4 needs the deprecation fixed first; the new features can wait until the plugin requires 8.4.

Problem: Sites that switched to PHP 8.4 show deprecation notices from a plugin: “Implicitly marking parameter $args as nullable is deprecated”. The team also wants to know which 8.4 features are worth using in new code.

Solution: Replace Type $x = null with ?Type $x = null everywhere (Rector or PHPCS can do it automatically), then, in code that requires PHP 8.4, use property hooks for validated properties, public private(set) for read-only-from-outside state, array_find() and array_any() for searches, and #[\Deprecated] for the plugin’s own deprecated functions.

The deprecation: implicitly nullable types

// deprecated in PHP 8.4
function ha_render_box( WP_Post $post = null, array $args = null ) {}
// correct, works since PHP 7.1
function ha_render_box( ?WP_Post $post = null, ?array $args = null ) {}

A default value of null used to make the type nullable implicitly. PHP 8.4 emits a deprecation notice for every such signature when the file is compiled, so the notices appear even if the function is never called. The explicit ? works on all PHP versions a plugin is likely to support, so this fix needs no version check. Rector does it with ExplicitNullableParamTypeRector: upgrade plugin code with Rector.

Property hooks

final class Ha_Slot {
    public int $minutes {
        set( int $value ) {
            if ( $value < 15 || $value % 15 ) {
                throw new InvalidArgumentException( 'Slots are multiples of 15 minutes.' );
            }
            $this->minutes = $value;
        }
    }

    public string $label {
        get => sprintf( _n( '%d minute', '%d minutes', $this->minutes, 'textdomain' ), $this->minutes );
    }

    public function __construct( int $minutes ) {
        $this->minutes = $minutes;
    }
}

$slot = new Ha_Slot( 30 );
echo $slot->label;   // "30 minutes"
$slot->minutes = 20; // InvalidArgumentException

A set hook validates every write, a get hook computes a value on read. The property is used like a normal public property, which replaces many getter/setter pairs. $label has only a get hook and no backing value: it is a virtual property.

Asymmetric visibility

final class Ha_Import_Result {
    public private(set) int $created = 0;
    public private(set) array $errors = array();

    public function add_error( string $message ): void {
        $this->errors[] = $message;
    }
}

$result = new Ha_Import_Result();
echo $result->created;      // readable everywhere
// $result->created = 5;    // Error: cannot modify private(set) property

Unlike readonly (PHP 8.1), the class itself can change the value as often as needed; outside code can only read it.

Small helpers

$admin = array_find( $users, fn( WP_User $u ) => in_array( 'administrator', $u->roles, true ) );
$has_draft = array_any( $posts, fn( WP_Post $p ) => 'draft' === $p->post_status );
$all_paid  = array_all( $orders, fn( WC_Order $o ) => $o->is_paid() );

$name = ( new Ha_Slot( 45 ) )->label; // still works; PHP 8.4 also allows new Ha_Slot( 45 )->label

array_find() returns the first matching element or null; array_find_key() its key. Method calls directly on new without wrapping parentheses are new in 8.4.

Deprecate your own functions

#[\Deprecated( message: 'use ha_get_slots() instead', since: '3.0' )]
function ha_slots(): array {
    return ha_get_slots();
}

Calling it emits a standard E_USER_DEPRECATED notice with the message, which debugging tools show like PHP’s own deprecations. On PHP before 8.4 the attribute is ignored; WordPress’s _deprecated_function() remains the portable way.

NOTE: Property hooks, private(set) and new without parentheses are syntax: a file using them causes a parse error on PHP 8.3 and older. Use them only with Requires PHP: 8.4 in the plugin header. WordPress 6.7 and newer are compatible with PHP 8.4 (“beta support” in the handbook’s terms), so the decision depends on the plugin’s users, not on core. The previous release: PHP 8.3 for plugin developers.