This is due to the fact, that $user is an array instead of an object!?
That's right, since the endpoint GET Users
returns the list of users, in the provided example:
$users = $graph->createRequest("GET", "/users")
->setReturnType(\Microsoft\Graph\Model\User::class)
->execute();
$users
contains an array of objects of Microsoft\Graph\Model\User
type, and
$givenName = $users[0]->getGivenName();
gives GivenName
property of first item in array.
A specific user could be requested via GET /users/{id | userPrincipalName}
endpoint:
$user = $graph->createRequest("GET", "/users/{$userId}")
->setReturnType(\Microsoft\Graph\Model\User::class)
->execute();
In that case $user
object is of Microsoft\Graph\Model\User
type:
$givenName = $user->getGivenName();
Update
setReturnType
function accept Microsoft Graph API entity type name, in your example it appears Model\User
points to type which doesn't belong to Microsoft\Graph\Model
namespace and as a result JSON response is not getting deserialized into class instance.
Instead of
setReturnType(Model\User::class)
try to specify fully qualified class name:
setReturnType(\Microsoft\Graph\Model\User::class)