php - 如何抛出未实现错误

标签 php error-handling

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/

相关文章:

php - 如何使用 PHP 将多维数组转换为单个数组?

php - 在 PHP 和 MYsql 中保存引导时间选择器值

php - 使用 DB2 查询加入 mysql 数据进行插入

java - 为什么我运行这个程序时没有弹出一个框架?约格尔

php - 尝试使用 PDO 将表单数据插入数据库时​​不断出现错误

java - 无法从静态上下文引用非静态方法count(int)

ruby-on-rails - 使用 simple_form 自定义错误消息

php - 生成一个随机数但有一个扭曲

javascript - 使用 php 附加到页面末尾

php - PHP登录文件不起作用。继续返回登录页面而不是用户个人资料