3v4l.org

run code in 300+ PHP versions simultaneously
<?php // I declare that MyService needs two Parameters, one: an object, two: a scalar value class MyService { public function __construct(MyService $service, $value) { $this->service = $service; $this->value = $value; // pretty common right? } } // Now, in my controller, I simply want to be able to DI the MyService object automatically, but it requires a value, so I can't do that // instead, I'll either have to use a SETTER (which I dont' want to, because my library NEEDS $value as a constructor parameter and I'm not changing it), or a factory. Here's what I'll have to do with a factory: class MyServiceFactory { public function build($value) { return $provider->make(MyService, [':value' => $value]); } } // Now I DI this service factory into my controller... class Controller { public function indexAction(MyServiceFactory $factory, Request $req) { $userSuppliedValue = $request->getValue('postValue'); $factory->build($userSuppliedValue); // Note how the $userSuppliedValue, that MyService needs as a constructor parameter, ISNT KNOWN until INSIDE the function I'm already asking it to be DI'd into? } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iiLLM
function name:  (null)
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E > > RETURN                                                   1

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

End of function __construct

End of class MyService.

Class MyServiceFactory:
Function build:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iiLLM
function name:  build
number of ops:  9
compiled vars:  !0 = $value, !1 = $provider
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        INIT_METHOD_CALL                                         !1, 'make'
          2        FETCH_CONSTANT                                   ~2      'MyService'
          3        SEND_VAL_EX                                              ~2
          4        INIT_ARRAY                                       ~3      !0, '%3Avalue'
          5        SEND_VAL_EX                                              ~3
          6        DO_FCALL                                      0  $4      
          7      > RETURN                                                   $4
   19     8*     > RETURN                                                   null

End of function build

End of class MyServiceFactory.

Class Controller:
Function indexaction:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iiLLM
function name:  indexAction
number of ops:  10
compiled vars:  !0 = $factory, !1 = $req, !2 = $userSuppliedValue, !3 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        INIT_METHOD_CALL                                         !3, 'getValue'
          3        SEND_VAL_EX                                              'postValue'
          4        DO_FCALL                                      0  $4      
          5        ASSIGN                                                   !2, $4
   27     6        INIT_METHOD_CALL                                         !0, 'build'
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0          
   30     9      > RETURN                                                   null

End of function indexaction

End of class Controller.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.02 ms | 1395 KiB | 13 Q