property_exists
Checks whether a class or an object has a property.
Syntax
property_exists(object|string $object_or_class, string $property): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $object_or_class | object|string | An object or a class name. |
| $property | string | The property name. |
Return value
bool`true` if the property is declared.
Examples
A property holding null exists:
<?php
class User
{
public ?string $email = null;
}
$user = new User();
var_dump(property_exists($user, 'email'));
var_dump(isset($user->email));Output:
bool(true)
bool(false)Notes
isset() returns false for a property holding null, while property_exists() returns true.
See also
- method_exists — Checks whether a class or an object has a method.
- isset — Checks that a variable exists and is not null.
In the tutorial
- Classes and objects — What classes and objects are in PHP, how to describe properties and methods and create objects.