Skip to content

Instantly share code, notes, and snippets.

@myanmarlinks
Created August 8, 2017 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myanmarlinks/6da60b25ce9ffaf8ff9d49da05fba5d6 to your computer and use it in GitHub Desktop.
Save myanmarlinks/6da60b25ce9ffaf8ff9d49da05fba5d6 to your computer and use it in GitHub Desktop.
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