Step-by-step writing of PHP's Framework (ix)

Source: Internet
Author: User
Tags exit exception handling log root directory

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:

1 <?php
2 Class Baseexception extends Exception {
3 Public Function Printstack () {
4
5 }
6 Public Function __tostring () {
7
8 }
9 }

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);
3 }

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 ');
4 }

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 ();
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 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.



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.