Home > Software engineering >  How to check if a variable $user is a User object, in PHP?
How to check if a variable $user is a User object, in PHP?

Time:10-13

I have little confusion around how to check the $user is a User object in Laravel.

$user = Apiato::call('User@FindUsersbytask', [$request->id]);
if($user) {   // here i have to check weather the $user is a user object or not
    Apiato::call('User@deleteuserTask', [$request->id]);
    ...

CodePudding user response:

Try this way

if(is_object($user))

CodePudding user response:

If what you want is to make sure the variable $user is an instance of the class User, then:

if ($user instanceof User) {
    ...
}

Otherwise, if it's just supposed to be any object, then you can use:

if (is_object($user)) {
    ...
}

although this will be true for an instance of any object, so you can't safely call methods or access properties that are present on the User class, for example.

  • Related