Possible Duplicate:
Throw a NotImplementedError in PHP?
在编写基类时,我经常逐渐开发,并且只构建我需要的(或者,我正在测试的)。但我喜欢我的界面保持平衡。对于每个 getter 都有一个 setter。在实现 CRUD 时,即使现在,我只需要 R,我宁愿先为 CUD 编写 stub 。
我希望它们抛出一些未实现的异常。我如何提出此类错误或异常?
例如:
class Resource {
/**
* get Issues a GET-request to Online, fetch with $this::$id
* @returns $this
*/
protected function get() {
if ($this->id) {
$attributes = http_build_query(array("id" => $this->id));
$url = "{$this->url}?{$attributes}";
$options = array_merge(array('method' => 'GET'));
$response = _http_request($url, $options);
$this->parse($response);
}
return $this;
}
/**
* post Place a new object on Online
*
* @param $attributes ....
* @returns Source $this
*/
protected function post($attributes = array()) {
throw new NotImplementedError();
}
/**
* put Updates an object on Online
*
* @param $attributes ....
* @returns $this
*/
protected function put($attributes = array()) {
throw new NotImplementedError();
}
/**
* delete Removes an object from Online, selected by $this::$id.
* @returns $this
*/
protected function delete() {
throw new NotImplementedError();
}
}
最佳答案
看看之前的答案:Throw a NotImplementedError in PHP?
简单地说,PHP 中不存在异常,但您可以通过扩展现有的异常来轻松创建自己的异常。前面的答案建议使用 BadMethodCallException 来执行此操作:
class NotImplementedException extends BadMethodCallException
{}
然后您可以在代码中抛出 NotImplementedException。
关于php - 如何抛出未实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13141373/