Skip to content

Instantly share code, notes, and snippets.

@grahamc
Created August 8, 2011 18:21
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 grahamc/1132345 to your computer and use it in GitHub Desktop.
Save grahamc/1132345 to your computer and use it in GitHub Desktop.
<?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