Previously talked about the exception processing mechanism, it is best to customize the exception, if debug open, then display specific debugging information, if debug shutdown, then the Exception log, and then jump to the error page, this lesson I will mainly talk about this exception handling mechanism.
Now we define an exception base class, in the Toper for tp_exception, here, I directly call Baseexception bar, that baseexception.php must first of all have a shelf:
2 |
Class Baseexception extends Exception { |
3 |
Public Function Printstack () { |
6 |
Public Function __tostring () { |
Most basic, this class is definitely going to inherit from Exception, and then I need to implement the __tostring method and another custom Printstack method, the reason for implementing __tostring method is often we will directly throw new Exception () This way throws an exception, it calls the __tostring () method directly, and the call to the Printstack method is the output stack information.
Say the idea, first of these two methods we need to determine whether is the debug mode, if the debug mode, then very easy, direct output can, otherwise, jump to the error page, but before jumping, you must write the exception information to the log file.
You might have noticed that, whether it's __tostring or printstack, if debug is turned off, you need to log debug information to the logfile, so we need to write an auxiliary function first:
1 |
protected function _tologfile ($STR) { |
2 |
File_put_contents (App_path. '/log ', $str, file_append); |
Here, in order to be simple, I directly use file_put_contents, this function is very powerful Ah, I particularly like ~ ~
Because so far there is no URL redirection, forwarding requests, and so on, temporarily directly display the error page, so you need to have an auxiliary function:
1 |
protected function _outputerrorpage () { |
2 |
Header ("content-type:text/html"); |
3 |
Echo file_get_contents (App_path. '/error.html '); |
Because the code is simple, the error pages and log files are written directly to the project root directory, and if you are interested, you can try to write the path in the configuration file, which is not difficult.
Because the auxiliary function is already written, the other two functions are easy:
1 |
Public Function Printstack () { |
2 |
if (true = = C (' Debug ')) { |
3 |
Echo parent::gettraceasstring (); |
5 |
$this->_tologfile (parent::gettraceasstring ()); |
6 |
$this->_outputerrorpage (); |
1 |
Public Function __tostring () { |
2 |
if (True!== C (' Debug ')) { |
3 |
$this->_tologfile (parent::gettraceasstring ()); |
4 |
$this->_outputerrorpage (); |
7 |
return parent::__tostring (); |
Of course, because __tostring to return a string, and once the throw an exception, it will display the exception information directly, in order to debug closed after the failure of the page to display the exception information, so directly exit here, of course, I do not think of a better way, So it is recommended to use Printstack to do it.
I am the exception class is used to stimulate, so if you have any good method, please and I more exchanges, learn from each other and grow up with each other.
Of course, now just defines a base class for exception handling, followed by a lot of subclasses of the exception, when it is really used to call its subclasses.