【问题标题】:"Wrong parameters for Exception" when creating Exception subclass创建异常子类时的“异常参数错误”
【发布时间】:2014-02-10 17:48:27
【问题描述】:

这是我的代码

class MyException extends Exception
{
   public function __construct($message, $code=0, Exception $previous = null) {
   parent::__construct($message,$code,$previous);
  }
   public function __toString() {
   return __CLASS__. ":[{$this->code}]:{$this->message}\n";
  }
}

这是错误

Fatal error: Wrong parameters for Exception([string $exception [, long $code ]]) on line 5

表示在这条线上

parent::__construct($message,$code,$previous);

出了什么问题?

【问题讨论】:

  • previous 参数是在 PHP 5.3 中添加的。看来您使用的是旧 PHP 版本

标签: php exception parameters


【解决方案1】:

如 cmets 中所述,在 the Exception constructor page 上,$previous 参数是在 PHP 5.3 中添加的。

这反映在错误消息中:

异常参数错误([string $exception [, long $code ]])

PHP 在确保构造函数完全匹配方面有点热心。异常是内部位的重要组成部分,完全正确处理它们值得迂腐。

This code demo 显示了旧 PHP 版本处理错误参数计数的各种方式。从上面使用的措辞来看,您使用的是 PHP 5.1 或 5.2。

请注意,在撰写本文时(2014 年 2 月),PHP 5.4 之前的版本已终止安全和错误修复更新,5.4 计划 EOL @987654323 @。

如果可以,请确保在部署应用程序时使用的是现代版本的 PHP。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多