# RFC: StackFrame class

- Status: 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
- Original: https://wiki.php.net/rfc/stack-frame-class
- Page: https://externals.io/rfc/stack-frame-class
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Add object-based debug_backtrace() alternative? (closed)

Yes: 14 · No: 14

Voters: alcaeus (No), alec (No), asgrim (Yes), ashnazg (No), brzuchal (Yes), bwoebi (No), carusogabriel (Yes), derick (No), galvao (Yes), geekcom (Yes), guilhermeblanco (Yes), jasny (Yes), jhdxr (No), kalle (No), marandall (Yes), nicolasgrekas (No), nikic (No), ocramius (Yes), pollita (No), ramsey (Yes), reywob (Yes), sebastian (Yes), sergey (Yes), svpernova09 (No), tandre (No), twosee (No), wyrihaximus (Yes), zimt (No)

### Replace object-based trace for Throwable::getTrace()? (closed)

Yes: 6 · No: 19

Voters: alcaeus (No), alec (No), asgrim (Yes), ashnazg (No), brzuchal (Yes), bwoebi (No), carusogabriel (No), derick (No), dragoonis (No), galvao (No), guilhermeblanco (Yes), jasny (No), kalle (No), marandall (No), nicolasgrekas (No), nikic (No), ocramius (Yes), ramsey (Yes), sebastian (No), sergey (No), svpernova09 (No), tandre (No), twosee (No), wyrihaximus (Yes), zimt (No)

## Discussions on the mailing list

- [\[RFC\]\[DISCUSSION\] debug_backtrace alternative as an array of StackFrame objects](https://externals.io/message/110868.md) — started on 2020-07-07, 14 messages
- [\[RFC\]\[VOTE\] debug_backtrace alternative as an array of StackFrame objects](https://externals.io/message/111091.md) — started on 2020-07-21, 5 messages

---

## 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.

```php
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()`.

```php
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

- [PR](https://github.com/php/php-src/pull/5820)
