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. interface Something { public function bar(); public function baz(); public function bat(); } interface SomethingFluent extends Something { public function bar(); public function baz(); public function bat(); } class Foo implements Something { public function bar() { var_dump(__METHOD__); } public function baz() { var_dump(__METHOD__); } public function bat() { var_dump(__METHOD__); return 'bat!'; } } class FluentWrapper implements SomethingFluent { private $instance; private $fluentMethods; public function __construct($instance, array $fluentMethods = array()) { $this->instance = $instance; $this->fluentMethods = $fluentMethods; } public function __call($name, $args) { $result = call_user_func(array($this->instance, $name), $args); return in_array($name, $this->fluentMethods) ? $this : $result; } } class FluentFoo extends FluentWrapper { public function __construct(Foo $foo) { parent::__construct($foo, array('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/YUlRU
function name:  (null)
number of ops:  24
compiled vars:  !0 = $foo, !1 = $bat
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   DECLARE_CLASS                                            'somethingfluent'
   20     1        DECLARE_CLASS                                            'foo'
   40     2        DECLARE_CLASS                                            'fluentwrapper'
   58     3        DECLARE_CLASS                                            'fluentfoo', 'fluentwrapper'
   67     4        NEW                                              $2      'FluentFoo'
          5        NEW                                              $3      'Foo'
          6        DO_FCALL                                      0          
          7        SEND_VAR_NO_REF_EX                                       $3
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !0, $2
   70    10        INIT_METHOD_CALL                                         !0, 'bar'
         11        DO_FCALL                                      0  $7      
   71    12        INIT_METHOD_CALL                                         $7, 'baz'
         13        DO_FCALL                                      0  $8      
   72    14        INIT_METHOD_CALL                                         $8, 'bar'
         15        DO_FCALL                                      0  $9      
   73    16        INIT_METHOD_CALL                                         $9, 'baz'
         17        DO_FCALL                                      0  $10     
   74    18        INIT_METHOD_CALL                                         $10, 'bat'
         19        DO_FCALL                                      0  $11     
   69    20        ASSIGN                                                   !1, $11
   76    21        CONCAT                                           ~13     !1, '%0A'
         22        ECHO                                                     ~13
         23      > RETURN                                                   1

Class Something:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YUlRU
function name:  bar
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E > > 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/YUlRU
function name:  baz
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E > > 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/YUlRU
function name:  bat
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E > > RETURN                                                   null

End of function bat

End of class Something.

Class SomethingFluent:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YUlRU
function name:  bar
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E > > 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/YUlRU
function name:  baz
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E > > 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/YUlRU
function name:  bat
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E > > RETURN                                                   null

End of function bat

End of class SomethingFluent.

Class Foo:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YUlRU
function name:  bar
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abar'
          2        DO_ICALL                                                 
   25     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/YUlRU
function name:  baz
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abaz'
          2        DO_ICALL                                                 
   30     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/YUlRU
function name:  bat
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   INIT_FCALL                                               'var_dump'
          1        SEND_VAL                                                 'Foo%3A%3Abat'
          2        DO_ICALL                                                 
   36     3      > RETURN                                                   'bat%21'
   37     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/YUlRU
function name:  __construct
number of ops:  7
compiled vars:  !0 = $instance, !1 = $fluentMethods
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   46     2        ASSIGN_OBJ                                               'instance'
          3        OP_DATA                                                  !0
   47     4        ASSIGN_OBJ                                               'fluentMethods'
          5        OP_DATA                                                  !1
   48     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/YUlRU
function name:  __call
number of ops:  21
compiled vars:  !0 = $name, !1 = $args, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   52     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
   54     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
   55    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/YUlRU
function name:  __construct
number of ops:  6
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   62     1        INIT_STATIC_METHOD_CALL                                  
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0          
   63     5      > RETURN                                                   null

End of function __construct

End of class FluentFoo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.01 ms | 1404 KiB | 17 Q