Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP Magic Method __isset() and __unset()
<?php
class Dog {
public $data = [];
public function __set($property, $value) {
$this->data[$property] = $value;
}
public function __isset($property) {
echo "isset triggered! <br>";
return isset($this->data[$property]);
}
public function __unset($property) {
echo "unset triggered! <br>";
unset($this->data[$property]);
}
}
$dog = new Dog();
var_dump(isset($dog->color));
$dog->color = "Black";
var_dump(isset($dog->color));
unset($dog->color);
var_dump(isset($dog->color));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment