Created
August 8, 2017 18:02
-
-
Save myanmarlinks/6da60b25ce9ffaf8ff9d49da05fba5d6 to your computer and use it in GitHub Desktop.
PHP Magic Method __isset() and __unset()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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