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

Source: Internet
Author: User
Tags config exception handling header ini log php file php and

Since the front controller controls everything, then we can use it to do more things!!

We know that in the Windows default php.ini display_errors open, and in Linux by default is turned off, so this is very cumbersome to debug the program, so we can set a debug property in the configuration file, which represents whether the debug open, If you turn on debugging, the program will have any exceptions, then output a lot of debugging information. Here, I'm just very simple to set up, the real framework debugging function is certainly not so simple.

First we implement if debug turns on, display debug Information debug mode: Such a string, and if the program has syntax or other errors, displays an error message and if debugging is turned off, then any error messages are not displayed.

Now modify the Frontcontroller __construct function:

01 Private Function __construct () {
02 C (Config::factory (Config::P hp)); Write configuration information
03 Session_Start ();
04 if (true = = C (' Debug ')) {
05 Echo ' debug mode: ';
06 Ini_set (' display_errors ', ' on ');
07 Error_reporting (C (' errorreporting '));
08 } else {
09 error_reporting (0);
10 Ini_set (' display_errors ', ' off ');
11 }
12 }

In this function, the main new debugging features and opened the session by default, because the configuration of PHP in the error is mainly the error_reporting function and php.ini in the Display_errors this item, so only need to set these two, No matter what the operating system is, you can properly control the debugging information.

Anyone who has written a PHP program might be aware that the default time zone for PHP is not Chinese, so if you use the DATE function to take out the current timestamp, you will find that it is not right, so you need to specify the time zone explicitly, which can be done entirely by the framework, as long as you write the value of the time zone in the configuration file. The framework then calls Date_default_timezone_set this function to set the time zone.

In this case, Frontcontroller's __construct This function becomes the following:

01 Private Function __construct () {
02 C (Config::factory (Config::P hp)); Write configuration information
03 Session_Start ();
04 Date_default_timezone_set (C (' timezone '));
05 if (true = = C (' Debug ')) {
06 Echo ' debug mode: ';
07 Ini_set (' display_errors ', ' on ');
08 Error_reporting (C (' errorreporting '));
09 } else {
10 error_reporting (0);
11 Ini_set (' display_errors ', ' off ');
12 }
13 }

If you have seen the toper source of FrontController.class.php This file, you will notice that the file code is still quite a lot, not as I write here now, only 20 lines, this is actually because a framework of the Frontcontroller also need There are many other things to do, such as preventing CSRF attacks, supporting custom configuration items, and so on, which I can't write for space reasons.

Set debug mode in the real framework is not just output debug mode: This is just a string, we must be clear.

People who have studied Java know that in Java all objects have a base class object, can you use a base class in a frame?

What functions does this base class do, such as when a method that does not exist for a class is invoked, so it will invoke the magic method of __call, and if we override this method in the base class, then all other classes inherit this class, and when they call the methods of these classes, if they do not exist, they naturally go to this method, You can do some remedial work in this method, which is better than the direct input method.

This base class is called base in the meantime, and in Toper, this base class is called TP.

First paste out the source code of the base.php:

01 <?php
02 Class Base {
03 Public Function __call ($name, $arguments) {
04 if (true = = C (' Debug ')) {
05 Echo ' Not exists method: ';
06 Echo ' The name is: ';
07 Var_dump ($name);
08 Echo ' The arguments is: ';
09 Var_dump ($arguments);
10 }
11 throw new Exception (' Not Exists method ');
12 }
13 }

Temporary function to write a simple point, if debugging open, then the nonexistent method name and parameter output, so that the developer is more likely to find the location of the error, of course, regardless of whether debugging open, exception must be thrown. Of course, on the online, if this problem, can be directly to the exception information displayed on the page, can not, it is best to output the exception information to the log file, and then the page to jump to the error page, so it is best we customize a set of exception handling classes, these classes inherit from exception, Then determine if debug open, if you turn on debug, then direct output information, otherwise, the exception information output to the log file, and then jump to the error page, specifically how to do, you can try!!!

I don't know if you noticed, all the PHP files I wrote I didn't write PHP terminator.

Actually it's very simple, I'll give you an example:

Now there are a.php and b.php,a.php The source code is this:

1 <?php
2 This place seems to have no output.
3 ?>

Then b.php's source code is this:

1 <?php
2 Include a.php
3 Session_Start ();
4 ?>

If you run it, you will notice that it will throw a warning that header already send out, why?

The main is a.php after the PHP end of the call there are a few lines of white space, then PHP will think it is HTML code, so it is not in the call Session_Start before the header output, we know, call Session_Start, is not able to have any output, so the program throws a warning.

If used? ", then we may inadvertently make this mistake, when the project is large, large amount of code, to find such a problem is difficult, so that the maintenance of the code to bring serious problems, so, the best in the final PHP file do not write?" This is also zend the official recommendation of the practice.



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.