Skip to content

Instantly share code, notes, and snippets.

@ssmusoke
Created May 12, 2012 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssmusoke/2667165 to your computer and use it in GitHub Desktop.
Save ssmusoke/2667165 to your computer and use it in GitHub Desktop.
_call method for Getters and Setters
/**
* Execute getters and setters on the instance if they do not exist
*
* @param String $method The method to execute
* @param Array $args The method arguments
*
* @throws Exception if the property does not exist
*/
function __call($method, $args){
$attribute = strtolower(substr($method, 3, 1).substr($method, 4));
$prefix = substr($method, 0, 3);
switch ($prefix) {
case "get":
# return the value of the attribute
return $this->$attribute;
break;
case "set":
# set the value of the attribute
$this->$attribute = $args[0];
break;
default:
# throw an exception
throw new Exception(sprintf('No property named `%1$s` exists for method %2$s', $attribute, $method));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment