Last active
March 31, 2017 11:06
-
-
Save maynagashev/d2ebd1b04d2ad4bc59eca13fe906ea30 to your computer and use it in GitHub Desktop.
Magic methods PHP __get/__set/__call
This file contains hidden or 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 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); | |
} | |
} |
This file contains hidden or 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
/** | |
* @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