The use of G method in the new feature of ThinkPHP3.1

  • 2021-07-02 23:39:50
  • OfStack

For a long time, ThinkPHP needs debug_start, debug_end methods and even Debug classes to complete those debugging functions. However, in ThinkPHP3.1, these complex functions were replaced by a simple G method, which is a gorgeous upgrade.

The function of G method includes two functions: marking position and interval statistics. Let's look at the specific usage below:

1. Mark location

The first use of the G method is to mark the position, for example:


G('begin');

Indicates that the current location is marked as an begin label and records the execution time of the current location and memory usage if the environment supports it. You can call the G method tag anywhere.

2. Runtime statistics

After marking the position, we can call G method again for interval statistics, for example:


G('begin');
 // ... Other code snippets 
G('end');
 // ... Maybe there are other codes here 
 //  Carry out statistical interval 
echo G('begin','end').'s';

G ('begin', 'end') indicates the execution time (in seconds) from begin position to end position. begin must be a marked position. If end position has not been marked at this time, the current position will be automatically marked as end tag, and the output result is similar to:


0.0056s

The default statistical accuracy is 4 decimal places. If you think this statistical accuracy is not enough, you can also set it for example:


G('begin','end',6).'s';

The possible output will become:


0.005587s

3. Memory overhead statistics

If your environment supports memory usage statistics, you can also use the G method for interval memory overhead statistics (in kb), such as:


echo G('begin','end','m').'kb';

The third parameter uses the m representation for memory overhead statistics, and the output may be:


625kb

Similarly, if the end tag is not marked, the current position will be automatically marked as the end tag first.
If the environment does not support memory statistics, the parameter is invalid and interval runtime statistics are still performed.

Forget debug_start, debug_end, the road to Jane, you know ~


Related articles: