“异常的错误参数";创建Exception子类时 [英] "Wrong parameters for Exception" when creating Exception subclass

查看:184
本文介绍了“异常的错误参数";创建Exception子类时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

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中.

As noted in the comments, and on the Exception constructor page, the $previous argument was added in PHP 5.3.

这反映在错误消息中:

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

PHP对于确保构造函数完全匹配 有点热情.异常是内部位的重要组成部分,正确地正确获得它们是值得学究的.

PHP is being a bit zealous in making sure that the constructor matches exactly. Exceptions are an important part of the internal bits, and getting them exactly right is worth being pedantic.

此代码演示显示了旧版本的PHP处理错误的参数计数的各种方式.根据上面的措辞,您正在使用PHP 5.1或5.2.

This code demo shows the various ways older PHP versions treat the incorrect parameter count. From the wording used above, you're using PHP 5.1 or 5.2.

请注意,在撰写本文档时(2014年2月),PHP 5.4之前的版本已达到安全性和错误修复更新的生命周期,并且5.4计划用于EOL 在2014年夏季.

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.

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

If you can, please make sure that you're using a modern version of PHP when deploying your application.

这篇关于“异常的错误参数";创建Exception子类时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆