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