How to verify if object variable is set

oop, php

Solution

`isset()` should work, object or not. You can also use

if ((isset($user)) and ($user instanceof User))

to check whether it is set and whether it is an object of the class `User`.

Problem

I have a `User` class which is created when a user logs in ``` $user = new User($userId); ``` Now to check if a user is logged in, I tried doing ``` if (isset($user)) { // user is logged in } else { // user is not logged in } ``` However, `isset()` doesn't appear to work for objects? And I've also tried `is_object()`. Please advise! Hopefully there is a way to do this elegantly, perhaps ``` if ($user->isLoggedIn()) { } ``` Thanks for your time!

Original source

Related problems