3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait ImmutableFromParams { private $mutable = true; private function __construct(array $params) {var_dump(get_object_vars($this));die;//array_intersect($params, (get_object_vars($this)))); die; foreach (array_intersect($params, (get_object_vars($this))) as $key => $value) { $this->{'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key)))}($value); } unset($this->mutable); } public static function fromParams(array $params) { return new static($params); } public function __call($method, $args) { if (isset($this->mutable) && substr($method, 0, 3) == 'set' && property_exists($this, lcfirst(substr($method, 3)))) { $this->{lcfirst(substr($method, 3))} = reset($args); } } } interface DataTransferObjectInterface { public static function fromParams(array $params); public function asArray(); } abstract class AbstractDataTransferObject implements DataTransferObjectInterface { use ImmutableFromParams; public function asArray() { return get_object_vars($this); } } final class MyDataTransferObject extends AbstractDataTransferObject { protected $foo; protected $bar; protected function setBar($value) { $this->bar = $value; } } $foo = MyDataTransferObject::fromParams(array('foo' => 'foo', 'bar' => 'bar', 'meh' => 'meh')); $foo->setFoo('newfoo'); $foo->setDog('dog'); var_dump($foo->asArray()); interface ValueObjectInterface { public static function fromParams(array $params); public function __toString(); } abstract class AbstractValueObject implements ValueObjectInterface { use ImmutableFromParams; abstract public function __toString(); } final class MyValueObject extends AbstractValueObject { protected $bar; protected function setBar($value) { $this->bar = $value; } public function __toString() { return (string) $this->bar; } } $foo = MyValueObject::fromParams(array('foo' => 'foo', 'bar' => 'bar', 'meh' => 'meh')); $foo->setFoo('newfoo'); $foo->setDog('dog'); echo $foo; /* To Do: 1. Throw exception on attempted $foo->setFoo() of DTO. 2. Return new instance on attempted $foo->setFoo() of VO. 3. Implement an equality method for VO. 4. Add guard clause preventing string to array conversion in VO when nested array in $params. 5. Add guard for incomplete $params & throw exception (would require explicit NULLs in $params for permissable NULL values). Notes: - When we want to raise the bar and require all DTOs & VOs to enforce their own validation internally (using private setters), remove __call & references to $mutable from the Trait and limit add check for in_array($param, array_keys(get_object_vars($this)));. */
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  (null)
number of ops:  32
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   DECLARE_CLASS                                            'abstractdatatransferobject'
   48     1        DECLARE_CLASS                                            'mydatatransferobject', 'abstractdatatransferobject'
   59     2        INIT_STATIC_METHOD_CALL                                  'MyDataTransferObject', 'fromParams'
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0  $1      
          5        ASSIGN                                                   !0, $1
   61     6        INIT_METHOD_CALL                                         !0, 'setFoo'
          7        SEND_VAL_EX                                              'newfoo'
          8        DO_FCALL                                      0          
   62     9        INIT_METHOD_CALL                                         !0, 'setDog'
         10        SEND_VAL_EX                                              'dog'
         11        DO_FCALL                                      0          
   64    12        INIT_FCALL                                               'var_dump'
         13        INIT_METHOD_CALL                                         !0, 'asArray'
         14        DO_FCALL                                      0  $5      
         15        SEND_VAR                                                 $5
         16        DO_ICALL                                                 
   66    17        DECLARE_CLASS                                            'valueobjectinterface'
   73    18        DECLARE_CLASS                                            'abstractvalueobject'
   80    19        DECLARE_CLASS                                            'myvalueobject', 'abstractvalueobject'
   95    20        INIT_STATIC_METHOD_CALL                                  'MyValueObject', 'fromParams'
         21        SEND_VAL_EX                                              <array>
         22        DO_FCALL                                      0  $7      
         23        ASSIGN                                                   !0, $7
   97    24        INIT_METHOD_CALL                                         !0, 'setFoo'
         25        SEND_VAL_EX                                              'newfoo'
         26        DO_FCALL                                      0          
   98    27        INIT_METHOD_CALL                                         !0, 'setDog'
         28        SEND_VAL_EX                                              'dog'
         29        DO_FCALL                                      0          
  100    30        ECHO                                                     !0
  112    31      > RETURN                                                   1

Class ImmutableFromParams:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 79) Position 1 = -2
filename:       /in/1ljhN
function name:  __construct
number of ops:  41
compiled vars:  !0 = $params, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'get_object_vars'
          3        FETCH_THIS                                       ~3      
          4        SEND_VAL                                                 ~3
          5        DO_ICALL                                         $4      
          6        SEND_VAR                                                 $4
          7        DO_ICALL                                                 
          8      > EXIT                                                     
    9     9*       INIT_FCALL                                               'array_intersect'
         10*       SEND_VAR                                                 !0
         11*       INIT_FCALL                                               'get_object_vars'
         12*       FETCH_THIS                                       ~6      
         13*       SEND_VAL                                                 ~6
         14*       DO_ICALL                                         $7      
         15*       SEND_VAR                                                 $7
         16*       DO_ICALL                                         $8      
         17*       FE_RESET_R                                       $9      $8, ->38
         18*       FE_FETCH_R                                       ~10     $9, !1, ->38
         19*       ASSIGN                                                   !2, ~10
   11    20*       INIT_FCALL                                               'str_replace'
         21*       SEND_VAL                                                 '+'
         22*       SEND_VAL                                                 ''
         23*       INIT_FCALL                                               'ucwords'
         24*       INIT_FCALL                                               'str_replace'
         25*       SEND_VAL                                                 '_'
         26*       SEND_VAL                                                 '+'
         27*       SEND_VAR                                                 !2
         28*       DO_ICALL                                         $12     
         29*       SEND_VAR                                                 $12
         30*       DO_ICALL                                         $13     
         31*       SEND_VAR                                                 $13
         32*       DO_ICALL                                         $14     
         33*       CONCAT                                           ~15     'set', $14
         34*       INIT_METHOD_CALL                                         ~15
         35*       SEND_VAR_EX                                              !1
         36*       DO_FCALL                                      0          
    9    37*       JMP                                                      ->18
         38*       FE_FREE                                                  $9
   14    39*       UNSET_OBJ                                                'mutable'
   15    40*     > RETURN                                                   null

End of function __construct

Function fromparams:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  fromParams
number of ops:  6
compiled vars:  !0 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   19     1        NEW                          static              $1      
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   20     5*     > RETURN                                                   null

End of function fromparams

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 25
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 38
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 25
Branch analysis from position: 11
filename:       /in/1ljhN
function name:  __call
number of ops:  39
compiled vars:  !0 = $method, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        ISSET_ISEMPTY_PROP_OBJ                           ~2      'mutable'
          3      > JMPZ_EX                                          ~2      ~2, ->11
          4    >   INIT_FCALL                                               'substr'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 0
          7        SEND_VAL                                                 3
          8        DO_ICALL                                         $3      
          9        IS_EQUAL                                         ~4      $3, 'set'
         10        BOOL                                             ~2      ~4
         11    > > JMPZ_EX                                          ~2      ~2, ->25
         12    >   INIT_FCALL                                               'property_exists'
         13        FETCH_THIS                                       ~5      
         14        SEND_VAL                                                 ~5
         15        INIT_FCALL                                               'lcfirst'
         16        INIT_FCALL                                               'substr'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 3
         19        DO_ICALL                                         $6      
         20        SEND_VAR                                                 $6
         21        DO_ICALL                                         $7      
         22        SEND_VAR                                                 $7
         23        DO_ICALL                                         $8      
         24        BOOL                                             ~2      $8
         25    > > JMPZ                                                     ~2, ->38
   26    26    >   INIT_FCALL                                               'lcfirst'
         27        INIT_FCALL                                               'substr'
         28        SEND_VAR                                                 !0
         29        SEND_VAL                                                 3
         30        DO_ICALL                                         $9      
         31        SEND_VAR                                                 $9
         32        DO_ICALL                                         $10     
         33        INIT_FCALL                                               'reset'
         34        SEND_REF                                                 !1
         35        DO_ICALL                                         $12     
         36        ASSIGN_OBJ                                               $10
         37        OP_DATA                                                  $12
   28    38    > > RETURN                                                   null

End of function __call

End of class ImmutableFromParams.

Class DataTransferObjectInterface:
Function fromparams:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  fromParams
number of ops:  2
compiled vars:  !0 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function fromparams

Function asarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  asArray
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E > > RETURN                                                   null

End of function asarray

End of class DataTransferObjectInterface.

Class AbstractDataTransferObject:
Function asarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  asArray
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   INIT_FCALL                                               'get_object_vars'
          1        FETCH_THIS                                       ~0      
          2        SEND_VAL                                                 ~0
          3        DO_ICALL                                         $1      
          4      > RETURN                                                   $1
   45     5*     > RETURN                                                   null

End of function asarray

End of class AbstractDataTransferObject.

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

End of function setbar

End of class MyDataTransferObject.

Class ValueObjectInterface:
Function fromparams:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  fromParams
number of ops:  2
compiled vars:  !0 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function fromparams

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  __toString
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   70     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function __tostring

End of class ValueObjectInterface.

Class AbstractValueObject:
Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  __toString
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function __tostring

End of class AbstractValueObject.

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

End of function setbar

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/1ljhN
function name:  __toString
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   FETCH_OBJ_R                                      ~0      'bar'
          1        CAST                                          6  ~1      ~0
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   92     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function __tostring

End of class MyValueObject.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.41 ms | 1404 KiB | 31 Q