3v4l.org

run code in 300+ PHP versions simultaneously
<?php // explicit mention of methods that should be fluent // would be preferable to returning $this on null // since a method may very well return null as something // that is expected. class Foo { public function bar() { var_dump(__METHOD__); } public function baz() { var_dump(__METHOD__); } public function bat() { var_dump(__METHOD__); return 'bat!'; } } class FluentWrapper { private $instance; private $fluentMethods; public function __construct($instance, array $fluentMethods = []) { $this->instance = $instance; $this->fluentMethods = $fluentMethods; } public function __call($name, $args) { $result = call_user_func([$this->instance, $name], $args); return in_array($name, $this->fluentMethods) ? $this : $result; } } class FluentFoo extends FluentWrapper { public function __construct(Foo $foo) { parent::__construct($foo, ['bar', 'baz']); } } $foo = new FluentFoo(new Foo()); $bat = $foo ->bar() ->baz() ->bar() ->baz() ->bat(); print $bat."\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  (null)
number of ops:  20
compiled vars:  !0 = $foo, !1 = $bat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   NEW                                              $2      'FluentFoo'
          1        NEW                                              $3      'Foo'
          2        DO_FCALL                                      0          
          3        SEND_VAR_NO_REF_EX                                       $3
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   58     6        INIT_METHOD_CALL                                         !0, 'bar'
          7        DO_FCALL                                      0  $7      
   59     8        INIT_METHOD_CALL                                         $7, 'baz'
          9        DO_FCALL                                      0  $8      
   60    10        INIT_METHOD_CALL                                         $8, 'bar'
         11        DO_FCALL                                      0  $9      
   61    12        INIT_METHOD_CALL                                         $9, 'baz'
         13        DO_FCALL                                      0  $10     
   62    14        INIT_METHOD_CALL                                         $10, 'bat'
         15        DO_FCALL                                      0  $11     
   57    16        ASSIGN                                                   !1, $11
   64    17        CONCAT                                           ~13     !1, '%0A'
         18        ECHO                                                     ~13
         19      > RETURN                                                   1

Class Foo:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  bar
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abar'
          2        DO_ICALL                                                 
   13     3      > RETURN                                                   null

End of function bar

Function baz:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  baz
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abaz'
          2        DO_ICALL                                                 
   18     3      > RETURN                                                   null

End of function baz

Function bat:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  bat
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abat'
          2        DO_ICALL                                                 
   24     3      > RETURN                                                   'bat%21'
   25     4*     > RETURN                                                   null

End of function bat

End of class Foo.

Class FluentWrapper:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  __construct
number of ops:  7
compiled vars:  !0 = $instance, !1 = $fluentMethods
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   34     2        ASSIGN_OBJ                                               'instance'
          3        OP_DATA                                                  !0
   35     4        ASSIGN_OBJ                                               'fluentMethods'
          5        OP_DATA                                                  !1
   36     6      > RETURN                                                   null

End of function __construct

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  __call
number of ops:  21
compiled vars:  !0 = $name, !1 = $args, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        FETCH_OBJ_R                                      ~3      'instance'
          3        INIT_ARRAY                                       ~4      ~3
          4        ADD_ARRAY_ELEMENT                                ~4      !0
          5        INIT_USER_CALL                                1          'call_user_func', ~4
          6        SEND_USER                                                !1
          7        DO_FCALL                                      0  $5      
          8        ASSIGN                                                   !2, $5
   42     9        INIT_FCALL                                               'in_array'
         10        SEND_VAR                                                 !0
         11        FETCH_OBJ_R                                      ~7      'fluentMethods'
         12        SEND_VAL                                                 ~7
         13        DO_ICALL                                         $8      
         14      > JMPZ                                                     $8, ->18
         15    >   FETCH_THIS                                       ~9      
         16        QM_ASSIGN                                        ~10     ~9
         17      > JMP                                                      ->19
         18    >   QM_ASSIGN                                        ~10     !2
         19    > > RETURN                                                   ~10
   43    20*     > RETURN                                                   null

End of function __call

End of class FluentWrapper.

Class FluentFoo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  __construct
number of ops:  6
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0          
   51     5      > RETURN                                                   null

End of function __construct

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/c9XaY
function name:  __call
number of ops:  21
compiled vars:  !0 = $name, !1 = $args, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   40     2        FETCH_OBJ_R                                      ~3      'instance'
          3        INIT_ARRAY                                       ~4      ~3
          4        ADD_ARRAY_ELEMENT                                ~4      !0
          5        INIT_USER_CALL                                1          'call_user_func', ~4
          6        SEND_USER                                                !1
          7        DO_FCALL                                      0  $5      
          8        ASSIGN                                                   !2, $5
   42     9        INIT_FCALL                                               'in_array'
         10        SEND_VAR                                                 !0
         11        FETCH_OBJ_R                                      ~7      'fluentMethods'
         12        SEND_VAL                                                 ~7
         13        DO_ICALL                                         $8      
         14      > JMPZ                                                     $8, ->18
         15    >   FETCH_THIS                                       ~9      
         16        QM_ASSIGN                                        ~10     ~9
         17      > JMP                                                      ->19
         18    >   QM_ASSIGN                                        ~10     !2
         19    > > RETURN                                                   ~10
   43    20*     > RETURN                                                   null

End of function __call

End of class FluentFoo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.16 ms | 1404 KiB | 17 Q