Created
September 10, 2015 06:03
-
-
Save tuanle/bc2b0889d367a1b31903 to your computer and use it in GitHub Desktop.
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
class MyClass { | |
// add one member var that holds all the data | |
private $myMagicData = array(); | |
private $another_var; | |
function __construct($data){ | |
$this->example = $data; // this will auto-call the __set function | |
$this->another_var = $data; // this won't, since $this->another_var was declared above | |
} | |
function __get($name) { | |
return array_key_exists($name, $this->myMagicData) ? $this->myMagicData[$name] : null; | |
} | |
function __set($name, $value) { | |
$this->myMagicData[$name] = $value; | |
} | |
function exampleFunction(){ | |
$test = $this->example; // this will auto-call the __get function | |
$another_test = $this->another_var; // this won't | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment