User->id() or Node->id() do not give back an integer but a string? I have written a custom class contructor (classname UserHandler) where I want to have a parameter which can be an instance of Drupal\user\Entity\User, an integer or FALSE:

  public function __construct($user = FALSE) {
    if ($user &amp;&amp; $user instanceof User ) {
      $this-&gt;account = $user;
    }

    if ($user &amp;&amp; is_int($user)) {
      $this-&gt;account = User::load($user);
    }


    if (!$user) {
      $this-&gt;account = User::load(Drupal::currentUser()-&gt;id());
    }

    $this-&gt;setProfileId();
  }


When I instantiate this class by calling UserHandler() or UserHandler($someUserClassInstance) everything works fine. When I want to instantiate this class by using a user id to load the user object in the constructor it works just fine as well: UserHandler(65).

But the problem appears when I want to use the id from any kind of entity or accountproxy class. Example in a hook_node_access:

function custom_node_access(NodeInterface $node, $op, AccountInterface $account) {
  $profile = new IgbUser($account-&gt;id());
}


$account is a class instance of Drupal\Core\Session\AccountInterface. When I call $account-&gt;id() the value returned is a string not an integer. This leads to the problem that a check like is_int($account-&gt;id()) becomes false. This appears with $node->id() as well for example.

Maybe I did not get some basics in PHP classes but all the comments above the id() methods in all the classes state that they would return an integer?!

Best,
Tobias
The value is in integer (for most content entity types, that is). The variable type however is a string.

That has a simple technical reason, values loaded from the database are always strings, those put into the node object and never explicitly cast to something else. Doing so would be too expensive for what it brings.