3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait LazyGetSet { private $useFluentSetter = true; /** * Change fluent setter. */ public function setFluentSetter($flag) { $this->useFluentSetter = $flag; } public function __call($method, $args) { if ((0 === strpos($method, 'get')) && ($ptyName = $this->getLazyPropertyName($method))) { return $this->lazyGet($ptyName, $args); } if ((0 === strpos($method, 'set')) && ($ptyName = $this->getLazyPropertyName($method))) { return $this->lazySet($ptyName, $args); } } /** * Detect property name from setter/getter methods. */ private function getLazyPropertyName($method) { $ptyName = lcfirst(substr($method, 3)); if (property_exists($this, $ptyName)) { return $ptyName; } } /** * Get property value */ private function lazyGet($ptyName) { return $this->{$ptyName}; } /** * Inject property value. * * @todo Validate data type */ private function lazySet($ptyName, $args) { if($this->validate($ptyName, $args[0])) { $this->{$ptyName} = $args[0]; } if ($this->useFluentSetter) { return $this; } } private function validate($variable_name, $variable_value) { $hints = $this->getPropertyHintType(__CLASS__, $variable_name); if(isset($hints['@var'])) { $validateCallback = array( 'string' => 'is_string', 'integer' => 'is_integer', 'array' => 'is_array', 'float' => 'is_float', 'double' => 'is_double', 'boolean' => 'is_bool', 'object' => 'is_object', 'resource' => 'is_resource', ); if(isset($validateCallback[$hints['@var']])) { return call_user_func($validateCallback[$hints['@var']], $variable_value); } else { return var_dump($variable_value instanceof $hints['@var']); } } } public function getPropertyHintType($className, $propertyName) { $class = new ReflectionClass($className); $match = array(); $docs = array(); foreach(explode("\n",(str_replace(array('*', '/'),'', $class->getProperty($propertyName)->getDocComment()))) as $doc) { $docs[] = trim($doc); } $return = array(); foreach(array_filter($docs) as $doc) { $match = array(); preg_match('/^([@].*)\ (.*)$/', $doc, $match); if($match){ $return[$match[1]] = trim($match[2]); } } return $return; } } class MyClass { use LazyGetSet; /** * @var integer */ private $name; /** * @var MyClass */ private $instant; } //echo (new MyClass())->setName(123)->getName();$MyClass = new MyClass();echo $MyClass->setInstant('a')->getInstant();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pb6il
function name:  (null)
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   DECLARE_CLASS                                            'myclass'
  119     1      > RETURN                                                   1

Class LazyGetSet:
Function setfluentsetter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pb6il
function name:  setFluentSetter
number of ops:  4
compiled vars:  !0 = $flag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        ASSIGN_OBJ                                               'useFluentSetter'
          2        OP_DATA                                                  !0
   14     3      > RETURN                                                   null

End of function setfluentsetter

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 19
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
Branch analysis from position: 13
filename:       /in/Pb6il
function name:  __call
number of ops:  37
compiled vars:  !0 = $method, !1 = $args, !2 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        INIT_FCALL                                               'strpos'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'get'
          5        DO_ICALL                                         $3      
          6        IS_IDENTICAL                                     ~4      $3, 0
          7      > JMPZ_EX                                          ~4      ~4, ->13
          8    >   INIT_METHOD_CALL                                         'getLazyPropertyName'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                           ~6      !2, $5
         12        BOOL                                             ~4      ~6
         13    > > JMPZ                                                     ~4, ->19
   19    14    >   INIT_METHOD_CALL                                         'lazyGet'
         15        SEND_VAR_EX                                              !2
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $7      
         18      > RETURN                                                   $7
   22    19    >   INIT_FCALL                                               'strpos'
         20        SEND_VAR                                                 !0
         21        SEND_VAL                                                 'set'
         22        DO_ICALL                                         $8      
         23        IS_IDENTICAL                                     ~9      $8, 0
         24      > JMPZ_EX                                          ~9      ~9, ->30
         25    >   INIT_METHOD_CALL                                         'getLazyPropertyName'
         26        SEND_VAR_EX                                              !0
         27        DO_FCALL                                      0  $10     
         28        ASSIGN                                           ~11     !2, $10
         29        BOOL                                             ~9      ~11
         30    > > JMPZ                                                     ~9, ->36
   23    31    >   INIT_METHOD_CALL                                         'lazySet'
         32        SEND_VAR_EX                                              !2
         33        SEND_VAR_EX                                              !1
         34        DO_FCALL                                      0  $12     
         35      > RETURN                                                   $12
   25    36    > > RETURN                                                   null

End of function __call

Function getlazypropertyname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pb6il
function name:  getLazyPropertyName
number of ops:  17
compiled vars:  !0 = $method, !1 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        INIT_FCALL                                               'lcfirst'
          2        INIT_FCALL                                               'substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 3
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                         $3      
          8        ASSIGN                                                   !1, $3
   33     9        INIT_FCALL                                               'property_exists'
         10        FETCH_THIS                                       ~5      
         11        SEND_VAL                                                 ~5
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $6      
         14      > JMPZ                                                     $6, ->16
   34    15    > > RETURN                                                   !1
   36    16    > > RETURN                                                   null

End of function getlazypropertyname

Function lazyget:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pb6il
function name:  lazyGet
number of ops:  4
compiled vars:  !0 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        FETCH_OBJ_R                                      ~1      !0
          2      > RETURN                                                   ~1
   44     3*     > RETURN                                                   null

End of function lazyget

Function lazyset:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/Pb6il
function name:  lazySet
number of ops:  17
compiled vars:  !0 = $ptyName, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   53     2        INIT_METHOD_CALL                                         'validate'
          3        SEND_VAR_EX                                              !0
          4        CHECK_FUNC_ARG                                           
          5        FETCH_DIM_FUNC_ARG                               $2      !1, 0
          6        SEND_FUNC_ARG                                            $2
          7        DO_FCALL                                      0  $3      
          8      > JMPZ                                                     $3, ->12
   54     9    >   FETCH_DIM_R                                      ~5      !1, 0
         10        ASSIGN_OBJ                                               !0
         11        OP_DATA                                                  ~5
   57    12    >   FETCH_OBJ_R                                      ~6      'useFluentSetter'
         13      > JMPZ                                                     ~6, ->16
   58    14    >   FETCH_THIS                                       ~7      
         15      > RETURN                                                   ~7
   60    16    > > RETURN                                                   null

End of function lazyset

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 28
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Pb6il
function name:  validate
number of ops:  29
compiled vars:  !0 = $variable_name, !1 = $variable_value, !2 = $hints, !3 = $validateCallback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   63     2        INIT_METHOD_CALL                                         'getPropertyHintType'
          3        FETCH_CLASS_NAME                                 ~4      
          4        SEND_VAL_EX                                              ~4
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $5      
          7        ASSIGN                                                   !2, $5
   64     8        ISSET_ISEMPTY_DIM_OBJ                         0          !2, '%40var'
          9      > JMPZ                                                     ~7, ->28
   65    10    >   ASSIGN                                                   !3, <array>
   75    11        FETCH_DIM_R                                      ~9      !2, '%40var'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          !3, ~9
         13      > JMPZ                                                     ~10, ->21
   76    14    >   FETCH_DIM_R                                      ~11     !2, '%40var'
         15        FETCH_DIM_R                                      ~12     !3, ~11
         16        INIT_USER_CALL                                1          'call_user_func', ~12
         17        SEND_USER                                                !1
         18        DO_FCALL                                      0  $13     
         19      > RETURN                                                   $13
         20*       JMP                                                      ->28
   78    21    >   INIT_FCALL                                               'var_dump'
         22        FETCH_DIM_R                                      ~14     !2, '%40var'
         23        FETCH_CLASS                                   0  $15     ~14
         24        INSTANCEOF                                       ~16     !1, $15
         25        SEND_VAL                                                 ~16
         26        DO_ICALL                                         $17     
         27      > RETURN                                                   $17
   81    28    > > RETURN                                                   null

End of function validate

Function getpropertyhinttype:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 30
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 30
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 30
2 jumps found. (Code = 77) Position 1 = 36, Position 2 = 52
Branch analysis from position: 36
2 jumps found. (Code = 78) Position 1 = 37, Position 2 = 52
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 51
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
Branch analysis from position: 51
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 52
Branch analysis from position: 30
filename:       /in/Pb6il
function name:  getPropertyHintType
number of ops:  55
compiled vars:  !0 = $className, !1 = $propertyName, !2 = $class, !3 = $match, !4 = $docs, !5 = $doc, !6 = $return
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   84     2        NEW                                              $7      'ReflectionClass'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $7
   85     6        ASSIGN                                                   !3, <array>
   86     7        ASSIGN                                                   !4, <array>
   87     8        INIT_FCALL                                               'explode'
          9        SEND_VAL                                                 '%0A'
         10        INIT_FCALL                                               'str_replace'
         11        SEND_VAL                                                 <array>
         12        SEND_VAL                                                 ''
         13        INIT_METHOD_CALL                                         !2, 'getProperty'
         14        SEND_VAR_EX                                              !1
         15        DO_FCALL                                      0  $12     
         16        INIT_METHOD_CALL                                         $12, 'getDocComment'
         17        DO_FCALL                                      0  $13     
         18        SEND_VAR                                                 $13
         19        DO_ICALL                                         $14     
         20        SEND_VAR                                                 $14
         21        DO_ICALL                                         $15     
         22      > FE_RESET_R                                       $16     $15, ->30
         23    > > FE_FETCH_R                                               $16, !5, ->30
   88    24    >   INIT_FCALL                                               'trim'
         25        SEND_VAR                                                 !5
         26        DO_ICALL                                         $18     
         27        ASSIGN_DIM                                               !4
         28        OP_DATA                                                  $18
   87    29      > JMP                                                      ->23
         30    >   FE_FREE                                                  $16
   90    31        ASSIGN                                                   !6, <array>
   91    32        INIT_FCALL                                               'array_filter'
         33        SEND_VAR                                                 !4
         34        DO_ICALL                                         $20     
         35      > FE_RESET_R                                       $21     $20, ->52
         36    > > FE_FETCH_R                                               $21, !5, ->52
   92    37    >   ASSIGN                                                   !3, <array>
   93    38        INIT_FCALL                                               'preg_match'
         39        SEND_VAL                                                 '%2F%5E%28%5B%40%5D.%2A%29%5C+%28.%2A%29%24%2F'
         40        SEND_VAR                                                 !5
         41        SEND_REF                                                 !3
         42        DO_ICALL                                                 
   94    43      > JMPZ                                                     !3, ->51
   95    44    >   FETCH_DIM_R                                      ~24     !3, 1
         45        INIT_FCALL                                               'trim'
         46        FETCH_DIM_R                                      ~26     !3, 2
         47        SEND_VAL                                                 ~26
         48        DO_ICALL                                         $27     
         49        ASSIGN_DIM                                               !6, ~24
         50        OP_DATA                                                  $27
   91    51    > > JMP                                                      ->36
         52    >   FE_FREE                                                  $21
   98    53      > RETURN                                                   !6
   99    54*     > RETURN                                                   null

End of function getpropertyhinttype

End of class LazyGetSet.

Class MyClass: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.96 ms | 1412 KiB | 33 Q