3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * PHP 5.6 example of "Functional Options" as presented by Rob Pike * in his article "Self-referential functions and the design of options" * http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html */ namespace Mailer { class Mailer { private $host; private $lazy = false; public function __construct($host, ...$options) { $this->host = $host; foreach ($options as $option) { $option($this); } } // Wish this could be a private function and still be // invoked by an options function so it's not mutable from the outside public function setLazy($lazy) { $this->lazy = $lazy; } public function isLazy() { return $this->lazy; } } } namespace Mailer\Options { class Lazy { public function __invoke(Mailer $mailer) { $mailer->setLazy(true); } } function SuperLazy() { return function(\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; } } namespace { use function \Mailer\Options\SuperLazy; $tls = function (\Mailer\Mailer $mailer) { echo 'LOL Lets do some TLS --'; }; // Any one of these $lazy vars can be passed as an option $lazy1 = function (\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; $lazy2 = new \Mailer\Options\Lazy; $lazy3 = SuperLazy(); // Ughhh, I want to do `$lazy3 = SuperLazy;` but PHP won't let me $mailer = new \Mailer\Mailer('www.gmail.com', $tls, $lazy3); echo $mailer->isLazy() ? 'Yes lazy' : 'No lazy'; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 21
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  (null)
number of ops:  24
compiled vars:  !0 = $tls, !1 = $lazy1, !2 = $lazy2, !3 = $lazy3, !4 = $mailer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   DECLARE_LAMBDA_FUNCTION                          ~5      [0]
          1        ASSIGN                                                   !0, ~5
   56     2        DECLARE_LAMBDA_FUNCTION                          ~7      [1]
          3        ASSIGN                                                   !1, ~7
   59     4        NEW                                              $9      'Mailer%5COptions%5CLazy'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $9
   60     7        INIT_FCALL                                               'mailer%5Coptions%5Csuperlazy'
          8        DO_FCALL                                      0  $12     
          9        ASSIGN                                                   !3, $12
   62    10        NEW                                              $14     'Mailer%5CMailer'
         11        SEND_VAL_EX                                              'www.gmail.com'
         12        SEND_VAR_EX                                              !0
         13        SEND_VAR_EX                                              !3
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !4, $14
   64    16        INIT_METHOD_CALL                                         !4, 'isLazy'
         17        DO_FCALL                                      0  $17     
         18      > JMPZ                                                     $17, ->21
         19    >   QM_ASSIGN                                        ~18     'Yes+lazy'
         20      > JMP                                                      ->22
         21    >   QM_ASSIGN                                        ~18     'No+lazy'
         22    >   ECHO                                                     ~18
   65    23      > 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/tTreF
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $mailer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
   51     1        ECHO                                                     'LOL+Lets+do+some+TLS+--'
   52     2      > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $mailer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   RECV                                             !0      
   57     1        INIT_METHOD_CALL                                         !0, 'setLazy'
          2        SEND_VAL_EX                                              <true>
          3        DO_FCALL                                      0          
   58     4      > RETURN                                                   null

End of Dynamic Function 1

Function mailer%5Coptions%5Csuperlazy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  Mailer\Options\SuperLazy
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   DECLARE_LAMBDA_FUNCTION                          ~0      [0]
   43     1      > RETURN                                                   ~0
   44     2*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  Mailer\Options\{closure}
number of ops:  5
compiled vars:  !0 = $mailer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        INIT_METHOD_CALL                                         !0, 'setLazy'
          2        SEND_VAL_EX                                              <true>
          3        DO_FCALL                                      0          
   43     4      > RETURN                                                   null

End of Dynamic Function 0

End of function mailer%5Coptions%5Csuperlazy

Class Mailer\Mailer:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 11
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/tTreF
function name:  __construct
number of ops:  13
compiled vars:  !0 = $host, !1 = $options, !2 = $option
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   15     2        ASSIGN_OBJ                                               'host'
          3        OP_DATA                                                  !0
   17     4      > FE_RESET_R                                       $4      !1, ->11
          5    > > FE_FETCH_R                                               $4, !2, ->11
   18     6    >   INIT_DYNAMIC_CALL                                        !2
          7        FETCH_THIS                                       $5      
          8        SEND_VAR_EX                                              $5
          9        DO_FCALL                                      0          
   17    10      > JMP                                                      ->5
         11    >   FE_FREE                                                  $4
   20    12      > RETURN                                                   null

End of function __construct

Function setlazy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  setLazy
number of ops:  4
compiled vars:  !0 = $lazy
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        ASSIGN_OBJ                                               'lazy'
          2        OP_DATA                                                  !0
   26     3      > RETURN                                                   null

End of function setlazy

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

End of function islazy

End of class Mailer\Mailer.

Class Mailer\Options\Lazy:
Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tTreF
function name:  __invoke
number of ops:  5
compiled vars:  !0 = $mailer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        INIT_METHOD_CALL                                         !0, 'setLazy'
          2        SEND_VAL_EX                                              <true>
          3        DO_FCALL                                      0          
   37     4      > RETURN                                                   null

End of function __invoke

End of class Mailer\Options\Lazy.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.06 ms | 1007 KiB | 14 Q