Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP Magic Methods Program (2)
class Dog {
public $name;
public $data = [];
public function bark() {
echo "Bark! <br />";
public function __set($key, $value) {
$this->data[$key] = $value;
public function __get($key) {
if(array_key_exists($key, $this->data)) {
return $this->data[$key];
} else {
trigger_error("Your key and value does not set", E_USER_ERROR);
$dog = new Dog();
$dog->name = "Aung Net";
$dog->color = "Black";
echo $dog->color;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.