Skip to content

Instantly share code, notes, and snippets.

@tuanle
Created September 10, 2015 06:03
Show Gist options
  • Save tuanle/bc2b0889d367a1b31903 to your computer and use it in GitHub Desktop.
Save tuanle/bc2b0889d367a1b31903 to your computer and use it in GitHub Desktop.
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