Skip to content

Instantly share code, notes, and snippets.

@caferrari
Last active December 28, 2015 02:39
Show Gist options
  • Save caferrari/7429754 to your computer and use it in GitHub Desktop.
Save caferrari/7429754 to your computer and use it in GitHub Desktop.
A nice usage for __get and __set magic functions
Trait MagicGetSet
{
public function __set($key, $value)
{
$setMethod = 'set' . ucfirst($key);
if (method_exists($this, $setMethod)) {
return $this->$setMethod($value);
}
$this->$key = $value;
}
public function __get($key)
{
$getMethod = 'get' . ucfirst($key);
if (method_exists($this, $getMethod)) {
return $this->$getMethod($key);
}
return $this->$key;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment