我正在尝试将数组作为Exception传递。我后来将其编码为json。
try {
class myException extends Exception {
private $params;
public function setParams(array $params) { $this->params = $params; }
public function getParams() { return $this->params; }
}
throw new myException(
array(
'error' => array(
'status' => true,
'code' => '12',
'note' => 'Does NOT contain application/json'
)
)
);
} catch (myException $e) {
print_r($e->getParams());
}
但不断收到以下错误:
致命错误:未捕获错误:myException的参数错误([string] $ message [,long $ code [,Throwable $ previous = NULL]]])in /home/domain/script.php:79堆栈跟踪:
#0 /home/domain/script.php(79):例外 - > __ construct(数组)
第79行/home/domain/script.php中抛出#p {1}
甚至可以传递数组吗?
如果是这样,我的代码怎么了?
答案 0 :(得分:0)
Exception::__construct
需要一个字符串作为第一个参数。如果要传递数组,则应覆盖构造函数。
class myException extends Exception {
private $params;
public function setParams(array $params) { $this->params = $params; }
public function __construct(array $params = array(), $code = 0, Throwable $previous = NULL) {
$this->setParams($params);
parent::__construct('', $code, $previous);
}