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