Created
August 8, 2011 18:21
-
-
Save grahamc/1132345 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* In reality we should be creating our getters, setters, etc, but for the | |
* truely lazy among us... | |
* | |
* @param string $method | |
* @param array $arguments | |
* @return void | |
* @author Travis Black | |
*/ | |
public function __call($method, $arguments) | |
{ | |
if (strpos($method, 'set') === 0) { | |
$attribute = lcfirst(substr($method, 3)); | |
if (property_exists($this, $attribute) && count($arguments == 1)) { | |
$this->$attribute = $arguments[0]; | |
return true; | |
} elseif (!property_exists($this, $attribute)) { | |
$error = get_class($this) . "::$method does not exist."; | |
} elseif (!count($arguments == 1)) { | |
$error = "$method expects 1 argument. You passed " . count($arguments); | |
} | |
} elseif (strpos($method, 'get') === 0) { | |
$attribute = lcfirst(substr($method, 3)); | |
if (property_exists($this, $attribute) && count($arguments == 0)) { | |
return $this->$attribute; | |
} elseif (!property_exists($this, $attribute)) { | |
$error = get_class($this) . "::$method does not exist."; | |
} elseif (!count($arguments == 0)) { | |
$error = "$method expects 0 arguments. You passed " . count($arguments); | |
} | |
} elseif (strpos($method, 'has') === 0) { | |
$attribute = lcfirst(substr($method, 3)); | |
if (property_exists($this, $attribute) && count($arguments == 0)) { | |
return isset($this->$attribute) && !empty($this->$attribute); | |
} elseif (!property_exists($this, $attribute)) { | |
$error = get_class($this) . "::$method does not exist."; | |
} elseif (!count($arguments == 0)) { | |
$error = "$method expects 0 arguments. You passed " . count($arguments); | |
} | |
} elseif (strpos($method, 'is') === 0) { | |
$attribute = lcfirst(substr($method, 2)); | |
if (property_exists($this, $attribute) && count($arguments == 0) && is_bool($this->$attribute)) { | |
return $this->$attribute; | |
} elseif (!property_exists($this, $attribute)) { | |
$error = get_class($this) . "::$method does not exist."; | |
} elseif (!count($arguments == 0)) { | |
$error = "$method expects 1 argument. You passed " . count($arguments); | |
} elseif (!is_bool($this->$attribute)) { | |
$error = get_class($this) . "::$attribute is not a boolean"; | |
} | |
} else { | |
$error = "There was a problem with $method"; | |
} | |
trigger_error($error, E_USER_ERROR); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment