The __isset() magic method in PHP is automatically triggered when isset()
or empty() is used on inaccessible or non-existent object properties. It allows developers
to define custom behavior for property existence checks.
isset() and empty().__get() and __set().The __isset() method enables controlled property checking, useful for dynamic properties,
lazy loading, and encapsulation of private or protected data.
// Demonstrates usage of __isset() with dynamic properties
data[$name] = $value;
}
public function __get($name) {
return $this->data[$name] ?? null;
}
public function __isset($name) {
return isset($this->data[$name]);
}
}
$user = new User();
$user->name = "Meghraj";
if (isset($user->name)) {
echo "Property 'name' is set";
}
if (empty($user->email)) {
echo "Property 'email' is not set";
}
?>
The first condition confirms that name exists and is set. The second condition detects that
email does not exist, triggering __isset() and returning false.
You can experiment by adding more dynamic properties and testing them with isset() and
empty() to observe how __isset() controls the behavior.
__isset() with __get() and __set().Product class using an internal array.__get(), __set(), and __isset().empty().