Make WordPress Core

Changeset 63443


Ignore:
Timestamp:
09/02/2026 08:47:56 PM (2 months ago)
Author:
SergeyBiryukov
Message:

Code Modernization: Use the null coalescing assignment operator in singleton accessors.

Replaces the null-guard singleton pattern in get_instance() accessors with the null coalescing assignment operator (??=):

// Before
public static function get_instance() {
	if ( null === self::$instance ) {
		self::$instance = new self();
	}

	return self::$instance;
}


// After
public static function get_instance() {
	self::$instance ??= new self();

	return self::$instance;
}

Both forms are functionally identical; ??= is shorter and states the intent directly.

Why this is safe

  • ??= requires PHP 7.4, which is already the minimum supported version.
  • The operator short-circuits: the right-hand side is only evaluated when the left-hand side is null or not set, so the object is still instantiated exactly once.
  • The operator is already used elsewhere in core, e.g. in wp-includes/l10n.php and wp-includes/view-config.php, so this only makes existing usage consistent.
  • No behavior change.

Intentionally not changed

WP_Abilities_Registry::get_instance() and WP_Ability_Categories_Registry::get_instance() use the same guard, but their blocks contain additional side effects beyond the assignment:

if ( null === self::$instance ) {
	self::$instance = new self();

	WP_Ability_Categories_Registry::get_instance();
	do_action( 'wp_abilities_api_init', self::$instance );
}

Converting these to ??= would cause the action and the nested registry initialization to run on every call instead of only on the first, so both are left as-is.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/12889.

Props Soean, mukesh27.
See #65823.

Location:
trunk/src
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r63342 r63443  
    7878         */
    7979        public static function get_instance() {
    80                 if ( null === self::$instance ) {
    81                         self::$instance = new WP_Site_Health();
    82                 }
     80                self::$instance ??= new WP_Site_Health();
    8381
    8482                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-bindings-registry.php

    r61642 r63443  
    273273         */
    274274        public static function get_instance() {
    275                 if ( null === self::$instance ) {
    276                         self::$instance = new self();
    277                 }
     275                self::$instance ??= new self();
    278276
    279277                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-pattern-categories-registry.php

    r61348 r63443  
    156156         */
    157157        public static function get_instance() {
    158                 if ( null === self::$instance ) {
    159                         self::$instance = new self();
    160                 }
     158                self::$instance ??= new self();
    161159
    162160                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r62201 r63443  
    281281         */
    282282        public static function get_instance() {
    283                 if ( null === self::$instance ) {
    284                         self::$instance = new self();
    285                 }
     283                self::$instance ??= new self();
    286284
    287285                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-styles-registry.php

    r61470 r63443  
    197197         */
    198198        public static function get_instance() {
    199                 if ( null === self::$instance ) {
    200                         self::$instance = new self();
    201                 }
     199                self::$instance ??= new self();
    202200
    203201                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-supports.php

    r62450 r63443  
    6161         */
    6262        public static function get_instance() {
    63                 if ( null === self::$instance ) {
    64                         self::$instance = new self();
    65                 }
     63                self::$instance ??= new self();
    6664
    6765                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-templates-registry.php

    r61431 r63443  
    243243         */
    244244        public static function get_instance() {
    245                 if ( null === self::$instance ) {
    246                         self::$instance = new self();
    247                 }
     245                self::$instance ??= new self();
    248246
    249247                return self::$instance;
  • trunk/src/wp-includes/class-wp-block-type-registry.php

    r62452 r63443  
    196196         */
    197197        public static function get_instance() {
    198                 if ( null === self::$instance ) {
    199                         self::$instance = new self();
    200                 }
     198                self::$instance ??= new self();
    201199
    202200                return self::$instance;
  • trunk/src/wp-includes/class-wp-icon-collections-registry.php

    r62748 r63443  
    219219         */
    220220        public static function get_instance() {
    221                 if ( null === self::$instance ) {
    222                         self::$instance = new self();
    223                 }
     221                self::$instance ??= new self();
    224222
    225223                return self::$instance;
  • trunk/src/wp-includes/class-wp-icons-registry.php

    r62821 r63443  
    376376         */
    377377        public static function get_instance() {
    378                 if ( null === self::$instance ) {
    379                         self::$instance = new self();
    380                 }
     378                self::$instance ??= new self();
    381379
    382380                return self::$instance;
  • trunk/src/wp-includes/fonts/class-wp-font-library.php

    r57686 r63443  
    134134         */
    135135        public static function get_instance() {
    136                 if ( null === self::$instance ) {
    137                         self::$instance = new self();
    138                 }
     136                self::$instance ??= new self();
    139137
    140138                return self::$instance;
  • trunk/src/wp-includes/l10n/class-wp-translation-controller.php

    r59029 r63443  
    6060         */
    6161        public static function get_instance(): WP_Translation_Controller {
    62                 if ( null === self::$instance ) {
    63                         self::$instance = new self();
    64                 }
     62                self::$instance ??= new self();
    6563
    6664                return self::$instance;
Note: See TracChangeset for help on using the changeset viewer.