3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** @template-covariant T */ class Store { /** @param T $value */ public function __construct(private $value) { } /** @return T */ public function getValue() { return $this->value; } } /** @template-covariant T */ class LazyValue { /** @var Store<T>|null */ private ?Store $store = null; /** @param Closure(): T $callable */ public function __construct(private Closure $callable) { } /** @return T */ public function getValue() { $store = $this->store ??= $this->doGetStore(); return $store->getValue(); } /** @return Store<T> */ private function doGetStore(): Store { $callable = $this->callable; $value = $callable(); return new Store($value); } } //------------------------ using it --------------------- function slow(): int { echo 'Doing slow calculation once'; return random_int(1, 42); } $lazy = new LazyValue(fn() => slow()); $lazy->getValue(); // prints: 'Doing slow calculation once', returns 'int' $lazy->getValue(); // does not print anything, only retuns 'int'
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  (null)
number of ops:  10
compiled vars:  !0 = $lazy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   NEW                                              $1      'LazyValue'
          1        DECLARE_LAMBDA_FUNCTION                          ~2      [0]
          2        SEND_VAL_EX                                              ~2
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
   62     5        INIT_METHOD_CALL                                         !0, 'getValue'
          6        DO_FCALL                                      0          
   63     7        INIT_METHOD_CALL                                         !0, 'getValue'
          8        DO_FCALL                                      0          
          9      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  {closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   INIT_FCALL                                               'slow'
          1        DO_FCALL                                      0  $0      
          2      > RETURN                                                   $0
          3*     > RETURN                                                   null

End of Dynamic Function 0

Function slow:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  slow
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   ECHO                                                     'Doing+slow+calculation+once'
   56     1        INIT_FCALL                                               'random_int'
          2        SEND_VAL                                                 1
          3        SEND_VAL                                                 42
          4        DO_ICALL                                         $0      
          5        VERIFY_RETURN_TYPE                                       $0
          6      > RETURN                                                   $0
   57     7*       VERIFY_RETURN_TYPE                                       
          8*     > RETURN                                                   null

End of function slow

Class Store:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  __construct
number of ops:  4
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'value'
          2        OP_DATA                                                  !0
   11     3      > RETURN                                                   null

End of function __construct

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  getValue
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   FETCH_OBJ_R                                      ~0      'value'
          1      > RETURN                                                   ~0
   17     2*     > RETURN                                                   null

End of function getvalue

End of class Store.

Class LazyValue:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  __construct
number of ops:  4
compiled vars:  !0 = $callable
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'callable'
          2        OP_DATA                                                  !0
   30     3      > RETURN                                                   null

End of function __construct

Function getvalue:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  getValue
number of ops:  12
compiled vars:  !0 = $store
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   FETCH_OBJ_IS                                     ~1      'store'
          1        COALESCE                                         ~2      ~1
          2        INIT_METHOD_CALL                                         'doGetStore'
          3        DO_FCALL                                      0  $3      
          4        ASSIGN_OBJ                                       ~4      'store'
          5        OP_DATA                                                  $3
          6        QM_ASSIGN                                        ~2      ~4
          7        ASSIGN                                                   !0, ~2
   37     8        INIT_METHOD_CALL                                         !0, 'getValue'
          9        DO_FCALL                                      0  $6      
         10      > RETURN                                                   $6
   38    11*     > RETURN                                                   null

End of function getvalue

Function dogetstore:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTDeh
function name:  doGetStore
number of ops:  12
compiled vars:  !0 = $callable, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   FETCH_OBJ_R                                      ~2      'callable'
          1        ASSIGN                                                   !0, ~2
   44     2        INIT_DYNAMIC_CALL                                        !0
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !1, $4
   46     5        NEW                                              $6      'Store'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
          8        VERIFY_RETURN_TYPE                                       $6
          9      > RETURN                                                   $6
   47    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function dogetstore

End of class LazyValue.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.72 ms | 1012 KiB | 15 Q