The feature illustrated below operates on PHP values, objects, or language metadata.
What Is property_exists in PHP?
Checks whether a property is declared on an object or class, including properties whose values are null; dynamic-property behavior depends on the object.
property_exists($object_or_class, string $property) : bool
Parameters:
$object_or_class— Required. The object_or_class argument used to configure this operation; its accepted values follow the declared PHP type.$property— Required. The property argument used to configure this operation; its accepted values follow the declared PHP type.
Return type: bool. A declared property can exist while its value is null or uninitialized.
Example 1: Understanding the Basic Behavior
<?php
class User { public string $name = 'Alex'; }
var_dump(property_exists(User::class, 'name'));
Output:
bool(true)
Explanation: Checks whether a property is declared on an object or class, including properties whose values are null; dynamic-property behavior depends on the object.
Example 2: Testing Another Case
<?php
class Account { private string $token = 'internal'; }
var_dump(property_exists(new Account(), 'token'));
Output:
bool(true)
Explanation: A declared property can exist while its value is null or uninitialized.
Practical Example: Applying the Operation
<?php
$object = new stdClass();
$object->status = null;
var_dump(property_exists($object, 'status'));
Output:
bool(true)
Explanation: Visibility and type declarations still govern reading or writing a property.
Important Behavior and Edge Cases
- A declared property can exist while its value is null or uninitialized.
- Visibility and type declarations still govern reading or writing a property.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
property_exists | Checks whether a property is declared on an object or class, including properties whose values are null; dynamic-property behavior depends on the object. |
isset() | property_exists() checks declaration/existence even for null values, while isset() returns false for null or uninitialized properties. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Using isset() as a substitute for testing whether a null-valued property is declared. | A declared property can exist while its value is null or uninitialized. |
| Choosing isset() without checking whether it provides the same result. | property_exists() checks declaration/existence even for null values, while isset() returns false for null or uninitialized properties. |
Key Takeaways
- Checks whether a property is declared on an object or class, including properties whose values are null; dynamic-property behavior depends on the object.
- A declared property can exist while its value is null or uninitialized.
- Visibility and type declarations still govern reading or writing a property.
The examples demonstrate where property_exists fits within php programming. Verify the relevant preconditions whenever you adapt the code to real data.