Skip to content

Instantly share code, notes, and snippets.

@myanmarlinks
Created July 9, 2017 05:36
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/de69f22325683889f9e7d8d777938c08 to your computer and use it in GitHub Desktop.
Save myanmarlinks/de69f22325683889f9e7d8d777938c08 to your computer and use it in GitHub Desktop.
PHP Magic Methods Program (2)
<?php
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