PHP 8.0 for WordPress developers: match, named arguments, nullsafe and more
PHP 8.0 was released on November 26, 2020. It brings the biggest set of language changes since PHP 7.0: the match expression, named arguments, the nullsafe operator, union types, constructor property promotion, and a JIT compiler. WordPress 5.6, due on December 8, ships with “beta support” for PHP 8.0: core works, but many plugins and themes will need fixes first.
Problem: You want to know which PHP 8.0 features help in plugin code, and what can break when a site moves to PHP 8.0.
Solution: Use match, the nullsafe operator, union types and constructor promotion in code that requires PHP 8.0; be careful with named arguments for WordPress functions; and test every plugin on PHP 8.0 before switching a live site, because many warnings became errors.
match instead of switch
<?php
function ha_status_label( string $status ): string {
return match ( $status ) {
'publish' => __( 'Published', 'textdomain' ),
'draft', 'auto-draft' => __( 'Draft', 'textdomain' ),
'pending' => __( 'Pending review', 'textdomain' ),
default => __( 'Unknown', 'textdomain' ),
};
}
match returns a value, compares strictly (===), has no fall-through, and throws an UnhandledMatchError when no arm matches and there is no default.
The nullsafe operator
<?php // PHP 7 $user = get_userdata( $user_id ); $email = $user ? $user->user_email : null; // PHP 8.0 $email = get_userdata( $user_id )?->user_email;
Careful: many WordPress functions return false, not null, on failure, and ?-> only protects against null. get_userdata() returns false for an unknown user, so the line above still produces a warning in that case. Use it with functions that return an object or null, like get_post().
Union types and constructor promotion
<?php
function ha_store_value( int $post_id, string $key, int|float|string $value ): bool {
return (bool) update_post_meta( $post_id, $key, $value );
}
final class HA_Report {
public function __construct(
private wpdb $db,
private string $table,
private int $limit = 50,
) {}
public function latest(): array {
return $this->db->get_results( "SELECT * FROM {$this->table} ORDER BY id DESC LIMIT {$this->limit}" );
}
}
Named arguments, with care
<?php // Skip optional parameters you do not need $html = wp_get_attachment_image( $id, size: 'large', attr: array( 'class' => 'hero' ) );
Named arguments make the parameter names part of the API. WordPress core does not promise to keep them stable, and a renamed parameter in a future release would break such calls. Use them freely for your own functions; for core functions, prefer positional arguments in code that must last.
What breaks on PHP 8.0
- Many warnings became
TypeErrororValueErrorexceptions: for example passing a string tocount()or a wrong type to a built-in function. - Comparisons between numbers and non-numeric strings changed:
0 == 'abc'is now false. - Required parameters after optional ones,
create_function(),each()and other removed functions cause fatal errors. - The
@operator no longer hides fatal errors.
Test locally or on a staging copy with PHP 8.0 and WP_DEBUG on, and check the error log. The PHPCompatibility rules for PHP_CodeSniffer scan code for removed and changed features.
NOTE: Code that uses PHP 8.0 syntax cannot even be parsed by PHP 7.4: the whole site shows a fatal error, not just your feature. A plugin that uses these features must declare Requires PHP: 8.0 in its header, which WordPress 5.2+ checks before activation, and keep its main file free of PHP 8 syntax so the check can run.