property_exists() is used to detect whether an object has a certain property, regardless of whether the value of the property is null , and it returns true as long as it exists (whether it is assigned or not).
grammar:
property_exists(object|string $object_or_class, string $property): bool
Example:
class User {
public $name;
private $email;
}
$user = new User();
var_dump(property_exists($user, 'name')); // true
var_dump(property_exists($user, 'email')); // true
var_dump(property_exists($user, 'age')); // false
As can be seen from the above example, property_exists() can detect a private property. As long as the property exists in the class, it will return true regardless of its visibility and whether it has been assigned.
advantage:
Can detect all visibility (public, protected, private) properties.
It doesn't care whether the attribute is assigned, even if it is null , it can be judged to exist.
shortcoming:
It is impossible to determine whether the attribute has been initialized, it can only determine whether it is declared.
isset() is more common in determining whether a variable is assigned and not null . It can also be used for the detection of object properties.
grammar:
isset(mixed $var): bool
Example:
class User {
public $name;
public $email = null;
}
$user = new User();
var_dump(isset($user->name)); // false
var_dump(isset($user->email)); // false
$user->name = 'Tom';
var_dump(isset($user->name)); // true
advantage:
Can determine whether the attribute is assigned and not null .
It is more commonly used in some logical judgments to confirm data availability.
shortcoming:
Private or protected properties cannot be detected (if accessed outside the class).
If the attribute exists but is null , the result is still false , which may cause misjudgment.
Compare | property_exists() | isset() |
---|---|---|
Whether the attribute exists | ? yes | ?? If the attribute is not assigned or is null, return false |
Is the attribute null | ? Not affected | ? Will cause false to be returned |
Private/protect attribute detection | ? Can | ? Cannot access outside the class |
Used for data judgment | ? Not recommended | ? Commonly used |
For reflection, debugging | ? More accurate | ? Not rigorous enough |
Scenario 1: Determine whether a certain attribute is declared in the class
Using property_exists() is more reliable:
if (property_exists($user, 'name')) {
echo "The attribute exists";
}
Scenario 2: Determine whether a certain attribute has been assigned
It is more appropriate to use isset() :
if (isset($user->name)) {
echo "The attribute has been assigned and not null";
}
Scenario 3: Detecting JSON mapping object properties through API
When processing JSON data mapping to PHP objects, the property may be null:
$json = '{"title": null}';
$data = json_decode($json);
var_dump(property_exists($data, 'title')); // true
var_dump(isset($data->title)); // false
This example clearly shows that property_exists() detects declarations, while isset() detects assignment status.
In most cases, the performance difference between the two is not large, but isset() is a language structure, slightly faster than property_exists() (function). However, considering the accuracy, it is recommended to choose functions with more explicit semantics when it comes to type judgment and object structure.
If the performance requirements are extremely high and the structure is guaranteed to be reliable, isset() can be used for rapid detection. However, if you need to rigorously detect property declarations, property_exists() should still be used.
In actual development, if you are building a RESTful API client, you need to determine whether the JSON response contains a specific field, even if the field value is null, then you should use property_exists() to ensure compatibility. For example, to determine whether there is an error message field:
if (property_exists($response, 'error')) {
// Handle error messages
}
If you only focus on data that makes sense for a field, such as non-empty strings or valid numbers, you can use isset() :
if (isset($response->data->url)) {
$url = $response->data->url;
header("Location: https://gitbox.net/redirect?url=" . urlencode($url));
}