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:
-
- // Enable the debugging mode
- Define ('app _ debug', true );
- // Load the framework entry file
- 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:
- '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:
- '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:
- '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:
- 'Trace _ PAGE_TABS '=> array (
- 'Base' => 'base ',
- 'File' => 'File ',
- 'Think' => 'process ',
- 'Error' => 'error ',
- 'SQL' => 'SQL ',
- 'Debug' => 'debug'
- )
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:
- 'Trace _ PAGE_TABS '=> array (
- 'Base' => 'base ',
- 'File' => 'File ',
- 'Think' => 'process ',
- 'Error' => 'error ',
- 'SQL' => 'SQL ',
- 'Debug' => 'debug ',
- 'User' => 'user'
- )
You can also copy the code to merge several tabs, for example:
- 'Trace _ PAGE_TABS '=> array (
- 'Base' => 'base ',
- 'File' => 'File ',
- 'Think' => 'process ',
- 'Error | debug | SQL '=> 'debug ',
- 'User' => 'user'
- )
Copy the code and debug the user information to the user tab. The trace method is used as follows:
- 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:
- '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:
- 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:
- '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:
- $ Info = 'Test information ';
- Trace ($ info, 'HS ');
If you want to debug and output the variables to a tab of the Trace page, you can use:
- 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
- 'Trace _ exception' => true
If you copy the code,
- 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:
- 'Err' // general error: general error
- 'Warn' // warning error: the error that requires a warning
- 'Notic '// notification: The program can run but it is not perfect.
- 'Info' // information: Program output information
- 'Debug' // DEBUG: DEBUG information
- '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:
- $ Blog = D ("Blog ");
- $ Blog = $ Blog-> find (3 );
- Dump ($ blog );
The result of copying the code in the browser is:
- Array (12 ){
- ["Id"] => string (1) "3"
- ["Name"] => string (0 )""
- ["User_id"] => string (1) "0"
- ["Cate_id"] => string (1) "0"
- ["Title"] => string (4) "test"
- ["Content"] => string (4) "test"
- ["Create_time"] => string (1) "0"
- ["Update_time"] => string (1) "0"
- ["Status"] => string (1) "0"
- ["Read_count"] => string (1) "0"
- ["Comment_count"] => string (1) "0"
- ["Tags"] => string (0 )""
- }
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:
- G ('begin ');
- //... Other code segments
- G ('end ');
- //... Maybe there is other code here
- // Statistical interval
- 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:
- 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:
- G ('BEGIN', 'End', 6).'s ';
The possible output of the copied code is:
- 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:
- Echo G ('BEGIN', 'End', 'M'). 'KB ';
The third parameter of the Copy code uses m for memory overhead statistics. the output result may be:
- 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:
- $ Blog = D ("Blog ");
- $ Vo = $ blog-> create ();
- Trace ($ vo, 'create V ');
- $ Vo = $ blog-> find ();
- Trace ($ vo, 'Find V ');
Copy code
If you need error debugging, use the following method to output the error message and interrupt the execution:
- 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:
- $ User = M ("User"); // instantiate the User object
- $ User-> find (1 );
- Echo $ User-> getLastSql ();
The output result of copying the code is
- 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.
- $ User = M ("User"); // instantiate the User model
- $ Info = M ("Info"); // instantiate the Info model
- $ User-> find (1 );
- $ Info-> find (2 );
- Echo M ()-> getLastSql ();
- Echo $ User-> getLastSql ();
- Echo $ Info-> getLastSql ();
The output result of copying the code is
- SELECT * FROM think_info WHERE id = 2
- SELECT * FROM think_user WHERE id = 1
- 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.