简体   繁体   English

创建Exception子类时“Exception的参数错误”

[英]“Wrong parameters for Exception” when creating Exception subclass

This is my code 这是我的代码

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";
  }
}

Here is the error 这是错误

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

means on this line 在这条线上意味着

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

What is going wrong? 出了什么问题?

As noted in the comments, and on the Exception constructor page , the $previous argument was added in PHP 5.3. 如注释中所述,在Exception构造函数页面上 ,在PHP 5.3中添加了$previous参数。

This is reflected in the error message: 这反映在错误消息中:

Wrong parameters for Exception ([string $exception [, long $code ]]) 异常的参数错误([string $ exception [,long $ code]])

PHP is being a bit zealous in making sure that the constructor matches exactly . PHP在确保构造函数完全匹配方面有点热心。 Exceptions are an important part of the internal bits, and getting them exactly right is worth being pedantic. 例外是内部位的重要组成部分,让它们完全正确是值得迂腐的。

This code demo shows the various ways older PHP versions treat the incorrect parameter count. 此代码演示显示了较旧的PHP版本处理错误参数计数的各种方法。 From the wording used above, you're using PHP 5.1 or 5.2. 从上面使用的措辞来看,您使用的是PHP 5.1或5.2。

Please be aware that at time of writing this (February 2014), versions of PHP prior to 5.4 have reached end of life for security and bug fix updates, and 5.4 is scheduled for EOL in summer 2014 . 请注意,在撰写本文时(2014年2月), 5.4之前的PHP版本已达到安全和错误修复更新的生命周期,并且计划在2014年夏季为EOL安装5.4。

If you can, please make sure that you're using a modern version of PHP when deploying your application. 如果可以,请确保在部署应用程序时使用的是现代版本的PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM