Compiling PHP Framework step by step (9)

Source: Internet
Author: User
We have talked about the exception handling mechanism before. it is best to customize the exception. if debug is enabled, the specific debugging information is displayed. if debug is disabled, the exception is recorded in the log, then jump to the error page. In this lesson, I will mainly talk about this exception handling mechanism .... "> <LINKhref =" http://www.php100.com//statics/style/he

 

We have talked about the exception handling mechanism before. it is best to customize the exception. if debug is enabled, the specific debugging information is displayed. if debug is disabled, the exception is recorded in the log, next, I will jump to the error page. In this lesson, I will mainly talk about this exception handling mechanism.

Now we define an exception base class. in Toper, it is Tp_Exception. here, we will call it BaseException directly. then BaseException. php must first have a shelf:

1
2 Class BaseException extends Exception {
3 Public function printStack (){
4
5 }
6 Public function _ toString (){
7
8 }
9 }

 

The most basic, this class must inherit from Exception, and then I need to implement the _ toString method and another custom printStack method, the reason for implementing the _ toString method is that we often directly throw new Exception (), which directly calls the _ toString () method, the printStack method is used to output the stack information.

Let's talk about the train of thought. First of all, we need to determine whether the two methods are in debug mode. if it is in debug mode, it is very easy to output directly. Otherwise, we will jump to the error page, but before the jump, you must write the exception information to the log file.

It may be noted that, whether it is _ toString or printStack, debugging information must be recorded to the log file if debug is disabled. Therefore, we need to first compile a helper function:

1 Protected function _ toLogFile ($ str ){
2 File_put_contents (APP_PATH. '/log', $ str, FILE_APPEND );
3 }

For ease of use, I can use file_put_contents directly. This function is very powerful and I like it very much ~~

 

Since URL redirection and forwarding requests have not yet been mentioned, an error page is displayed for the moment. Therefore, an auxiliary function is required:

1 Protected function _ outputErrorPage (){
2 Header ("content-type: text/html ");
3 Echo file_get_contents (APP_PATH. '/error.html ');
4 }

Because the code is simple, the error pages and log files are directly written to the project root directory. if you are interested, you can write the path in the configuration file by yourself, this is not very difficult.

 

Since the helper functions have been completed, the other two functions are very easy:

1 Public function printStack (){
2 If (true = C ('debug ')){
3 Echo parent: getTraceAsString ();
4 } Else {
5 $ This-> _ toLogFile (parent: getTraceAsString ());
6 $ This-> _ outputErrorPage ();
7 }
8 }

 

1 Public function _ toString (){
2 If (true! = C ('debug ')){
3 $ This-> _ toLogFile (parent: getTraceAsString ());
4 $ This-> _ outputErrorPage ();
5 Exit ();
6 }
7 Return parent ::__ toString ();
8 }

 

Of course, because _ toString needs to return a string, once throw an exception occurs, the exception information is displayed directly. in order not to display the exception information on the page after debug is disabled, therefore, exit directly here. of course, I have not come up with a better method, so we recommend using printStack.

I am using this exception class, so if you have any good methods, please communicate with me, learn from each other, and grow with each other.

Of course, now we only define a base class for exception handling. we will write a lot of subclasses about exceptions later, and call them when they are actually used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.