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