Created
May 12, 2012 15:27
-
-
Save ssmusoke/2667165 to your computer and use it in GitHub Desktop.
_call method for Getters and Setters
This file contains 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
/** | |
* 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