Skip to content

Null Coalesce on local static variable #17981

@Geolim4

Description

@Geolim4

Description

The following code:

  function getUserContext()
  {
    static $userContext ??= User::load(\Drupal::currentUser()->id());
    return $userContext;
  }
<?php

Resulted in this output:

syntax error, unexpected token "??=", expecting "," or ";"

But I expected this output instead:

Assignation if `$currentUser` is not yet defined.

PHP Version

PHP 8.3.0, PHP 8.4.0

Operating System

All

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions