The following command must be used to get the current user id for drupal 8 and drupal 9.
\Drupal::currentUser()->id();