Skip to content

Instantly share code, notes, and snippets.

@maynagashev
Last active March 31, 2017 11:06
Show Gist options
  • Save maynagashev/d2ebd1b04d2ad4bc59eca13fe906ea30 to your computer and use it in GitHub Desktop.
Save maynagashev/d2ebd1b04d2ad4bc59eca13fe906ea30 to your computer and use it in GitHub Desktop.
Magic methods PHP __get/__set/__call
class pinbaWrapper {
private $pinbaEnabled;
public function __construct() {
$this->pinbaEnabled = extension_loaded('pinba');
}
public function __call($name, $arguments) {
if (!$this->pinbaEnabled) return false;
return call_user_func_array($name, $arguments);
}
}
/**
* @param $key
*
* @return mixed
*/
public function __get($key) {
$method = "get" . ucfirst($key);
if (method_exists($this, $method)) {
return $this->$method();
} elseif (isset($this->data[$key])) {
return $this->data[$key];
} elseif ($key == '_id' && isset($this->data['id'])) {
return $this->data['id'];
}
}
/**
* @param $key
* @param $value
*/
public function __set($key, $value) {
$method = "set" . ucfirst($key);
if (method_exists($this, $method)) {
return $this->$method($value);
}
$this->data[$key] = $value;
$this->storeData();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment