rfc:debug_backtrace_depth

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:debug_backtrace_depth [2021/03/13 17:12] tandrerfc:debug_backtrace_depth [2021/03/13 18:10] tandre
Line 9: Line 9:
 ===== Introduction ===== ===== Introduction =====
  
-Inspecting the current stack trace depth is occasionally useful for manually debugging, checking for potential infinite recursion, or for checking if code would hit stack frame limits in extensions such as XDebug (''xdebug.max_nesting_limit''). It is currently possible to compute the depth through ''count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit=0))'', but this is verbose and inefficient compared to returning the depth directly.+Inspecting the current stack trace depth is occasionally useful for manually debugging, checking for potential infinite recursion, or for checking if code would hit stack frame limits in extensions such as Xdebug (''xdebug.max_nesting_limit''). It is currently possible to compute the depth through ''count(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit=0))'', but this is verbose and inefficient compared to returning the depth directly.
  
 ===== Proposal ===== ===== Proposal =====
Line 24: Line 24:
 When ''$limit <= 0'', there is no limit. When ''$limit <= 0'', there is no limit.
  
-Internally, PHP's stack frames are represented like a linked list (i.e. to count them, you need to iterate through the frames), and this RFC does not change that representation. The amount of time needed to compute the depth is proportional to the depth, which is why this RFC supports the optional ''$limit''. Counting the frames is still much faster than constructing an array with the ''file'', ''line'', ''class'', etc. with ''debug_backtrace()''.+Internally, PHP's stack frames are represented like a singly linked list (i.e. to count them, you need to iterate through the frames), and this RFC does not change that representation. The amount of time needed to compute the depth is proportional to the depth, which is why this RFC supports the optional ''$limit''. Counting the frames is still much faster than constructing an array with the ''file'', ''line'', ''class'', etc. with ''debug_backtrace()''.
  
 ==== Example use cases ==== ==== Example use cases ====
Line 53: Line 53:
 === Checking for compatibility with extensions with stack trace limits === === Checking for compatibility with extensions with stack trace limits ===
  
-For example, in XDebug https://xdebug.org/docs/all_settings#max_nesting_level has a default value of 256 - if you are developing an application or library which cannot override max_nesting_level you may want to ensure your library does not use deep recursion.+For example, in Xdebug https://xdebug.org/docs/all_settings#max_nesting_level has a default value of 256 - if you are developing an application or library which cannot override max_nesting_level you may want to ensure your library does not use deep recursion.
  
 <code php> <code php>
Line 59: Line 59:
 function my_common_function() { function my_common_function() {
     if (is_running_as_unit_test() && debug_backtrace_depth(250) >= 250) {     if (is_running_as_unit_test() && debug_backtrace_depth(250) >= 250) {
-        throw new TestFrameworkError("Refactor this code, it's likely to hit the default xdebug.max_nesting_level in other environments");+        throw new TestFrameworkError("Refactor this code, xdebug.max_nesting_level may be reached in other environments");
     }     }
     // body of my_common_function()     // body of my_common_function()
Line 107: Line 107:
 ===== Future Scope ===== ===== Future Scope =====
 The behavior of ''debug_backtrace'' and ''debug_print_backtrace'' could be changed to make $limit be consistent with the documentation in a separate RFC. https://www.php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters. The behavior of ''debug_backtrace'' and ''debug_print_backtrace'' could be changed to make $limit be consistent with the documentation in a separate RFC. https://www.php.net/manual/en/function.debug-backtrace.php#refsect1-function.debug-backtrace-parameters.
 +
 +===== Proposed PHP Version =====
 +8.1
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
 Yes/No, requiring a 2/3 majority Yes/No, requiring a 2/3 majority
rfc/debug_backtrace_depth.txt · Last modified: 2021/06/02 02:05 by tandre