ThinkPHP3.1 quick start (7) Debugging

Source: Internet
Author: User
This article describes how to debug ThinkPHP. This article describes how to debug ThinkPHP.

When it comes to debugging in the debugging mode, we may first think of the debugging mode. Yes, ThinkPHP also has a debugging mode specially set for the development process. The debugging mode of ThinkPHP is different, it will sacrifice a certain degree of execution efficiency, but the convenience and debugging functions are very worthwhile. We strongly recommend that ThinkPHP developers enable the debugging mode during the development phase (until the debugging mode is disabled after the formal deployment) to detect and analyze and solve hidden problems in a timely manner. [-More-]
To enable the debugging mode, you only need to add a constant definition code in the entry file:
  1. // Enable the debugging mode
  2. Define ('app _ debug', true );
  3. // Load the framework entry file
  4. Require './ThinkPHP. php ';
After you copy the code to the production environment in the development phase, you only need to delete the debugging mode and define the code to switch to the deployment mode. The debugging mode has the following advantages:
  • Enable logging. all error information and debugging information are recorded in detail to facilitate debugging;
  • Disable Template caching. template modifications take effect immediately;
  • Records SQL logs to facilitate SQL analysis;
  • Disable field caching. modifying data table fields is not affected by caching;
  • Strictly check file case sensitivity (even on Windows) to help you detect Linux deployment problems in advance;
  • It can be conveniently used in different stages of the development process, including development, testing, demonstration, and other situations. different application modes can be used to configure independent project configuration files;
When the debug mode is enabled, we can set different application statuses for the project and load different project configuration files. However, first, import the default debugging mode configuration file of the framework, which is located in the Conf \ debug Directory of the system directory. php.
In general, you can configure some development modes in the debugging configuration file. For example, configure additional database connections for debugging, enable log writing to facilitate searching for error information, and enable page Trace to output more debugging information.
If the application status is not configured, the system defaults to the debug status, that is, the default configuration parameters are:
  1. 'App _ status' => 'debug', // application debugging mode STATUS
Copy the code if debugging exists in the configuration Directory of the project. php file, the configuration file is automatically loaded, and merged with the system project configuration file and system debugging configuration file, that is, debug. in the php configuration file, you only need to configure different parameters or new parameters in the project configuration file and system debugging configuration file. If you want to add the application status in debug mode, for example, test status, you can change the settings in the project configuration file as follows:
  1. 'App _ status' => 'test', // application debugging mode STATUS
If the code is copied, the system will automatically try to load the test. php configuration file under the project configuration Directory. you can change the relevant settings in the test configuration file, such as changing the connection information of the test database.
Because the debugging mode does not have any cache, it involves a large number of file I/O operations and real-time template compilation. Therefore, when the debugging mode is enabled, the performance may decrease, however, the performance of the deployment mode is not affected. In addition, once the debugging mode is disabled, the debugging configuration file of the project becomes invalid immediately.

The page Trace debugging mode does not fully meet our debugging needs. sometimes we need to manually output some debugging information. In addition to debugging with some development tools, ThinkPHP also provides some built-in debugging tools and functions. For example, the page Trace function is an auxiliary tool provided by ThinkPHP to developers for development and debugging. Allows you to display the request information, running status, SQL execution, and error message of operations on the current page in real time, and supports custom display. The page Trace function is effective in both debugging and deployment modes, but it can only be used when there is page output (if your operation does not have any output, the page Trace function may not help you much, you may need to use the following debugging method ). However, in the deployment mode, the debugging information displayed is not complete in the debugging mode. generally, we recommend that you use the page Trace together with the debugging mode.
To enable the page Trace function, you must set it in the project configuration file:
  1. 'Show _ PAGE_TRACE '=> true, // display page Trace information
Copy the code. this parameter is disabled by default. if the template output is available on your page, the ThinkPHP LOGO is displayed in the lower right corner of the page:

The number following the LOGO is the execution time (in seconds) on the current page. click this icon to display detailed page Trace information,

The page Trace framework has six tabs: basic, File, process, error, SQL, and debugging. click different tabs to switch to different Trace information windows.
Basic: Basic summary information of the current page, such as execution time, memory overhead, number of file loads, and number of queries.
File: lists the files loaded during the execution of the current page and their sizes in detail.
Process: lists the actions and related processes on the current page (to be completed ).
Error: some error messages during the execution of the current page, including warning errors.
SQL: information about the SQL statements executed on the current page.
Debugging: Debugging output by developers in the program.
The Trace tab of the page can be customized and expanded. the default configuration is:
  1. 'Trace _ PAGE_TABS '=> array (
  2. 'Base' => 'base ',
  3. 'File' => 'File ',
  4. 'Think' => 'process ',
  5. 'Error' => 'error ',
  6. 'SQL' => 'SQL ',
  7. 'Debug' => 'debug'
  8. )
Copy the code, that is, the tab displayed by default. if you want to add a new tab: user, you can modify the configuration as follows:
  1. 'Trace _ PAGE_TABS '=> array (
  2. 'Base' => 'base ',
  3. 'File' => 'File ',
  4. 'Think' => 'process ',
  5. 'Error' => 'error ',
  6. 'SQL' => 'SQL ',
  7. 'Debug' => 'debug ',
  8. 'User' => 'user'
  9. )
You can also copy the code to merge several tabs, for example:
  1. 'Trace _ PAGE_TABS '=> array (
  2. 'Base' => 'base ',
  3. 'File' => 'File ',
  4. 'Think' => 'process ',
  5. 'Error | debug | SQL '=> 'debug ',
  6. 'User' => 'user'
  7. )
Copy the code and debug the user information to the user tab. The trace method is used as follows:
  1. Trace ($ user, 'user information', 'User ');
Copy the third parameter of the code to indicate the tab identifier, which corresponds to the one we configured in TRACE_PAGE_TABS.
By default, the information displayed in the Trace window on the page is not saved. to save the trace information, we can configure the PAGE_TRACE_SAVE parameter:
  1. 'Page _ TRACE_SAVE '=> true
After you copy the code to enable page trace information saving, each page Trace information is saved to the project's log directory in the form of logs. the name format is: current date _ trace. log, for example:
  1. 12-06-21_trace.log
Copy the code. if you do not want to save information about all tabs, you can set the tabs to save. for example:
  1. 'Page _ trace_save' => array ('base', 'file', 'SQL ');
After you copy the code settings, only information about the three tabs, base, file, and SQL, is saved.

The Trace method page Trace can only be used when there is page output, but the trace method can be used in any situation, and the trace method can be used for AJAX and other operations.
Trace method format: trace ('variable ', 'tag', 'level', 'whether to record log') for example:
  1. $ Info = 'Test information ';
  2. Trace ($ info, 'HS ');
If you want to debug and output the variables to a tab of the Trace page, you can use:
  1. Trace ($ info, 'hint', 'User ');
Copy the code to output it to the user tab. If no tab is specified, it is output to the debug tab by default.
The trace method can also directly throw an exception. if it is output to the ERR tab and enabled
  1. 'Trace _ exception' => true
If you copy the code,
  1. Trace ($ info, 'error', 'err ');
Copying code throws an exception.
In three cases, the trace method records logs:
  • AJAX request
  • SHOW_PAGE_TRACE is false, that is, when the page Trace is disabled
  • The fourth parameter of the trace method is true.
In this case, the third parameter of the trace method indicates the log level of the record, usually including:
  1. 'Err' // general error: general error
  2. 'Warn' // warning error: the error that requires a warning
  3. 'Notic '// notification: The program can run but it is not perfect.
  4. 'Info' // information: Program output information
  5. 'Debug' // DEBUG: DEBUG information
  6. 'SQL' // SQL: SQL statement
Copy code
Variable debugging outputs a variable that is often used during development. in addition to using the built-in var_dump and print_r of php, the ThinkPHP framework has a browser-friendly var_dump method, used to output the variable information to the browser.
Dump browser-friendly variable output
Usage dump ($ var, $ echo = true, $ label = null, $ strict = true)
Var (required): the variable to be output. all variable types are supported.
Echo (optional): whether to output directly. the default value is true. if the value is false, return but no output.
Label (optional): label of the variable output. it is null by default.
Strict (optional): type of the output variable. the default value is true. if the value is false, print_r is used for output.
Returns the string to be output if the echo parameter is false.
Example:
  1. $ Blog = D ("Blog ");
  2. $ Blog = $ Blog-> find (3 );
  3. Dump ($ blog );
The result of copying the code in the browser is:
  1. Array (12 ){
  2. ["Id"] => string (1) "3"
  3. ["Name"] => string (0 )""
  4. ["User_id"] => string (1) "0"
  5. ["Cate_id"] => string (1) "0"
  6. ["Title"] => string (4) "test"
  7. ["Content"] => string (4) "test"
  8. ["Create_time"] => string (1) "0"
  9. ["Update_time"] => string (1) "0"
  10. ["Status"] => string (1) "0"
  11. ["Read_count"] => string (1) "0"
  12. ["Comment_count"] => string (1) "0"
  13. ["Tags"] => string (0 )""
  14. }
Copy code
During Performance Debugging and development, sometimes, to test the performance, you often need to debug the running time or memory consumption of a code segment, the system provides the G method to conveniently obtain the running time and memory usage of a certain interval. For example:
  1. G ('begin ');
  2. //... Other code segments
  3. G ('end ');
  4. //... Maybe there is other code here
  5. // Statistical interval
  6. Echo G ('BEGIN', 'end').'s ';
Copy the code G ('BEGIN', 'end') to calculate the execution time from the begin position to the end position (in seconds). begin must be a marked position, if the end position is not marked at this time, the current position is automatically marked as the end label. the output result is similar:
  1. 0.0056 s
The default statistical precision of the Copy code is four digits after the decimal point. if you think the statistical accuracy is not enough, you can also set it as follows:
  1. G ('BEGIN', 'End', 6).'s ';
The possible output of the copied code is:
  1. 0.005587 s
Copy the code. if your environment supports memory usage statistics, you can also use the G Method for interval memory overhead statistics (unit: kb). For example:
  1. Echo G ('BEGIN', 'End', 'M'). 'KB ';
The third parameter of the Copy code uses m for memory overhead statistics. the output result may be:
  1. 625kb
Copy the code. if the end label is not marked, the current position is automatically marked with the end label first.
If the environment does not support memory statistics, this parameter is invalid and the interval running time statistics are still performed.

Breakpoint debugging relies on powerful page Trace information functions. ThinkPHP supports breakpoint debugging.
We only need to trace a variable at different locations, for example:
  1. $ Blog = D ("Blog ");
  2. $ Vo = $ blog-> create ();
  3. Trace ($ vo, 'create V ');
  4. $ Vo = $ blog-> find ();
  5. Trace ($ vo, 'Find V ');
Copy code
If you need error debugging, use the following method to output the error message and interrupt the execution:
  1. Halt ($ msg) // output the error message and stop the execution.
Copy code
During model debugging, to better identify errors, you often need to check the recently used SQL statements. we can use the getLastsql method to output the last executed SQL statement. For example:
  1. $ User = M ("User"); // instantiate the User object
  2. $ User-> find (1 );
  3. Echo $ User-> getLastSql ();
The output result of copying the code is
  1. SELECT * FROM think_user WHERE id = 1
Copy the code and each model uses an independent final SQL record, which does not affect each other. However, you can use the getLastSql method of the empty model to obtain the final global SQL record.
  1. $ User = M ("User"); // instantiate the User model
  2. $ Info = M ("Info"); // instantiate the Info model
  3. $ User-> find (1 );
  4. $ Info-> find (2 );
  5. Echo M ()-> getLastSql ();
  6. Echo $ User-> getLastSql ();
  7. Echo $ Info-> getLastSql ();
The output result of copying the code is
  1. SELECT * FROM think_info WHERE id = 2
  2. SELECT * FROM think_user WHERE id = 1
  3. SELECT * FROM think_info WHERE id = 2
The copy code getLastSql method can only obtain the last executed SQL records. to learn more about SQL logs, you can view the Trace or log file on the current page.

Note: The Mongo database driver does not execute SQL due to the special nature of the interface. Therefore, the SQL logging function is encapsulated and implemented. Therefore, for performance considerations, you can use the getLastSql method to obtain the last executed SQL record only when the debug mode is enabled.

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.