Throw an exception when an undefined method is called on one of our DB_DataObjects, instead of failing silently.

The magic __call() method is used to implement a getter and setter interface, and simply didn't bother to throw an error for things it didn't recognize.

This may expose a number of existing errors where mistyped method names are called and we're not noticing that they're failing.
This commit is contained in:
Brion Vibber 2010-03-19 12:38:14 -07:00
parent f23fc93a43
commit 5e54e7b55d

View File

@ -96,6 +96,30 @@ class Safe_DataObject extends DB_DataObject
$this->_link_loaded = false; $this->_link_loaded = false;
} }
/**
* Magic function called when someone attempts to call a method
* that doesn't exist. DB_DataObject uses this to implement
* setters and getters for fields, but neglects to throw an error
* when you just misspell an actual method name. This leads to
* silent failures which can cause all kinds of havoc.
*
* @param string $method
* @param array $params
* @return mixed
* @throws Exception
*/
function __call($method, $params)
{
$return = null;
// Yes, that's _call with one underscore, which does the
// actual implementation.
if ($this->_call($method, $params, $return)) {
return $return;
} else {
throw new Exception('Call to undefined method ' .
get_class($this) . '::' . $method);
}
}
/** /**
* Work around memory-leak bugs... * Work around memory-leak bugs...