gpt4 book ai didi

codeigniter - CodeIgniter 中的自定义异常

转载 作者:行者123 更新时间:2023-12-05 01:19:30 27 4
gpt4 key购买 nike

我想扩展调用 getMessage 时返回自定义消息的异常类。

class MY_Exceptions extends CI_Exceptions{
function __construct(){
parent::__construct();
}
function getMessage(){
$msg = parent::getMessage();
return "ERROR - ".$msg;
}
}

MY_Exceptions 放在核心文件夹中。并且异常抛出/处理如下:

try{
throw new Exception('a message');
}catch (Exception $e) {
echo $e->getMessage();
}

目的是获得“错误 - 一条消息”。但它总是返回“一条消息”。当我尝试调试时,控件永远不会转到 MY_Exception 类。有什么我想念的吗?

最佳答案

创建文件 core/MY_Exceptions.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Exceptions extends Exception {

public function __construct($message, $code = 0, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}

// custom string representation of object
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; //edit this to your need
}

}

class MyCustomExtension extends MY_Exceptions {} //define your exceptions

关于codeigniter - CodeIgniter 中的自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27449155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com