StackFrame class View as Markdown

Declined
Authors
Michał Marcin Brzuchalski, brzuchal@php.net
Date
2020-07-07
Version
1.2
Target
PHP 8.0
Implementation
https://github.com/php/php-src/pull/5820
Source
wiki.php.net/rfc/stack-frame-class

Votes

Add object-based debug_backtrace() alternative? closed
Yes
14
No
14
28 voters
alcaeus No 2020-08-03 11:00
alec No 2020-07-23 05:38
asgrim Yes 2020-07-22 10:23
ashnazg No 2020-07-24 15:23
brzuchal Yes 2020-07-21 11:52
bwoebi No 2020-07-22 11:44
carusogabriel Yes 2020-07-28 15:38
derick No 2020-07-21 13:45
galvao Yes 2020-07-23 07:41
geekcom Yes 2020-07-27 13:33
guilhermeblanco Yes 2020-07-22 15:14
jasny Yes 2020-07-24 12:54
jhdxr No 2020-07-26 09:55
kalle No 2020-07-22 01:14
marandall Yes 2020-07-21 16:25
nicolasgrekas No 2020-07-21 12:39
nikic No 2020-07-21 12:30
ocramius Yes 2020-07-21 11:55
pollita No 2020-08-02 21:22
ramsey Yes 2020-07-27 16:07
reywob Yes 2020-08-03 07:10
sebastian Yes 2020-07-22 06:49
sergey Yes 2020-07-22 13:24
svpernova09 No 2020-07-22 16:20
tandre No 2020-07-21 23:09
twosee No 2020-07-22 04:05
wyrihaximus Yes 2020-07-21 12:10
zimt No 2020-07-22 07:27
Replace object-based trace for Throwable::getTrace()? closed
Yes
6
No
19
25 voters
alcaeus No 2020-08-03 11:00
alec No 2020-07-23 05:38
asgrim Yes 2020-07-22 10:24
ashnazg No 2020-07-24 15:24
brzuchal Yes 2020-07-21 11:52
bwoebi No 2020-07-22 11:44
carusogabriel No 2020-07-22 21:54
derick No 2020-07-21 13:45
dragoonis No 2020-08-03 17:06
galvao No 2020-07-23 07:42
guilhermeblanco Yes 2020-07-22 15:15
jasny No 2020-07-24 12:54
kalle No 2020-07-22 01:13
marandall No 2020-07-21 16:25
nicolasgrekas No 2020-07-21 12:39
nikic No 2020-07-21 12:30
ocramius Yes 2020-07-21 11:57
ramsey Yes 2020-07-27 16:07
sebastian No 2020-07-22 06:50
sergey No 2020-07-22 13:24
svpernova09 No 2020-07-22 16:20
tandre No 2020-07-21 23:09
twosee No 2020-07-22 04:05
wyrihaximus Yes 2020-07-21 12:28
zimt No 2020-07-22 07:27

Discussions on the mailing list

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

The debug_backtrace() function currently returns stack trace frames as an array of arrays with information about the file, line, class, type, object, args and function name as keys. This RFC proposes to add a debug_backtrace() alternative which returns an array of objects instead. This reduces memory usage and makes code operating on frames more readable.

Proposal

Introduce new StackFrame class with static method getTrace() returning array of StackFrame object instances.

There are two places where this could be a replacement for retrieving trace in array of arrays way and these are ReflectionGenerator::getTrace() and Throwable::getTrace(). These should be a subject of the secondary vote.

StackFrame class

StackFrame class provides properties which mirrors information from debug_backtrace().

There are additional properties and methods which expose additional information:

  • property object_class exposes object class which is useful when $option passed without DEBUG_BACKTRACE_PROVIDE_OBJECT flag;
  • property closure exposes a closure within the frame was produced upon.
final class StackFrame implements ArrayAccess
{
    public ?string $file;
 
    public ?int $line;
 
    public ?string $function;
 
    public ?string $class;
 
    public ?object $object;
 
    public ?string $objectClass;
 
    public ?string $type;
 
    public ?\Closure $closure;
 
    public array $args = [];
 
    public static function getTrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array {}
}

Example

An example below shows exactly the same portion of information as if it would call debug_backtrace() instead of StackFrame::getTrace().

function frames() {
    return StackFrame::getTrace();
}
$frame = frames()[0];
 
var_dump([
    'file' => $frame['file'],
    'line' => $frame['line'],
    'function' => $frame['function'],
    'class' => $frame['class'],
    'type' => $frame['type'],
    'object' => $frame['object'],
    'args' => $frame['args'],
]);

Performance

On a test script with 1M recursions to produce huge result results were as above:

  1. StackFrame::getTrace() execution time was 0.299s and memory usage was only 192.96MB.
  2. debug_backtrace() execution time is 0.333s which is similar but the memory usage was 390.96MB.

Backward Incompatible Changes

If vote decides about changing Exception trace with array of StackTrace objects then Exception::getTrace() method will no longer be a mutable array of arrays with simple keys. Although a StackFrame implements ArrayAccess interface and allow to read all keys in BC manner manipulation on frames will no longer be possible.

Proposed PHP Version(s)

Next PHP 8.0

RFC Impact

To SAPIs

None.

To Existing Extensions

None.

To Opcache

None.

New Constants

None.

Proposed Voting Choices

As this is a change in exception handling mechanism it requires 2/3 accepted.

The vote will be a simple Yes/No for StackFrame inclusion and second vote a simple Yes/No for exception trace replacement.

Vote

Voting opened 2020-07-21 and closes 2020-08-04.

Vote: Add object-based debug_backtrace() alternative? — Yes: 14 · No: 14 (closed)

''''

Vote: Replace object-based trace for Throwable::getTrace()? — Yes: 6 · No: 19 (closed)

Implementation