rfc:stack-frame-class

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:stack-frame-class [2020/07/07 20:20] brzuchalrfc:stack-frame-class [2020/07/21 05:12] – $file and $line nullability brzuchal
Line 1: Line 1:
 ====== PHP RFC: StackFrame class ====== ====== PHP RFC: StackFrame class ======
-  * Version: 0.9+  * Version: 1.1
   * Date: 2020-07-07   * Date: 2020-07-07
-  * Author: Michał Brzuchalski, <brzuchal@php.net>+  * Author: Michał Marcin Brzuchalski, <brzuchal@php.net>
   * Status: Under Discussion   * Status: Under Discussion
   * Target Version: PHP 8.0   * Target Version: PHP 8.0
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-Introduce new ''StackFrame'' with static method ''getTrace()'' and optionally replace ''Exception'' class trace with  +Introduce new ''StackFrame'' class with static method ''getTrace()'' returning array of ''StackFrame'' object instances.
-array of ''StackFrame'' objects.+
  
-''StackFrame'' class provides properties and methods which mirrors information from ''debug_backtrace()'':+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 and methods which mirrors information from ''debug_backtrace()''
 + 
 +There are additional properties and methods which expose additional information: 
 + 
 +  * property ''object_class'' and ''getObjectClass(): ?string'' method which exposes object class which is useful when ''$option'' passed without ''DEBUG_BACKTRACE_PROVIDE_OBJECT'' flag; 
 +  * property ''closure'' and ''getClosure(): ?\Closure'' method which exposes a closure within the frame was produced upon.
  
 <code php> <code php>
 final class StackFrame implements ArrayAccess final class StackFrame implements ArrayAccess
 { {
-    public readonly string $file;+    public ?string $file;
          
-    public readonly int $line;+    public ?int $line;
          
-    public readonly ?string $function;+    public ?string $function;
          
-    public readonly ?string $class;+    public ?string $class;
          
-    public readonly ?object $object;+    public ?object $object
 + 
 +    public ?string $object_class; 
 + 
 +    public ?\Closure $closure;
          
-    public readonly array $args;+    public array $args;
          
-    public static function getTrace(): array {}+    public static function getTrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array {}
  
-    public function getFile(): string {}+    public function getFile(): ?string {}
  
-    public function getLine(): int {}+    public function getLine(): ?int {}
  
     public function getFunction(): ?string {}     public function getFunction(): ?string {}
Line 43: Line 55:
  
     public function getObject(): ?object {}     public function getObject(): ?object {}
 +    
 +    public function getObjectClass(): ?string {}
 +
 +    public function getClosure(): ?\Closure {}
  
     public function getType(): ?string {}     public function getType(): ?string {}
Line 49: Line 65:
 } }
 </code> </code>
 +
 +==== Example ====
 +An example below shows exactly the same portion of information as if it would call ''debug_backtrace()'' instead of ''StackFrame::getTrace()''.
 +
 +<code 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'],
 +]);
 +</code>
 +
 +==== Performance ====
 +On a test script with 1M recursions to produce huge result results were as above:
 +
 +  - ''StackFrame::getTrace()'' execution time was 0.299s and memory usage was only **192.96MB**.
 +  - ''debug_backtrace()'' execution time is 0.333s which is similar but the memory usage was **390.96MB**.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 68: Line 110:
  
 ==== New Constants ==== ==== New Constants ====
-Not yet.+None.
  
  
Line 77: Line 119:
  
 ===== Implementation ===== ===== Implementation =====
-...+  * [[https://github.com/php/php-src/pull/5820|PR]]
rfc/stack-frame-class.txt · Last modified: 2020/08/04 08:00 by brzuchal