PHP - warning - Undefined property: stdClass - fix? [duplicate]

Solution 1:

if(isset($response->records))
    print "we've got records!";

Solution 2:

In this case, I would use:

if (!empty($response->records)) {
 // do something
}

You won't get any ugly notices if the property doesn't exist, and you'll know you've actually got some records to work with, ie. $response->records is not an empty array, NULL, FALSE, or any other empty values.

Solution 3:

isset() is fine for top level, but empty() is much more useful to find whether nested values are set. Eg:

if(isset($json['foo'] && isset($json['foo']['bar'])) {
    $value = $json['foo']['bar']
}

Or:

if (!empty($json['foo']['bar']) {
    $value = $json['foo']['bar']
}

Solution 4:

You can use property_exists
http://www.php.net/manual/en/function.property-exists.php