3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL); ini_set('display_errors', 1); 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($MyClass)->getInstant();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0264V
function name:  (null)
number of ops:  18
compiled vars:  !0 = $MyClass
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'error_reporting'
          1        SEND_VAL                                                 32767
          2        DO_ICALL                                                 
    4     3        INIT_FCALL                                               'ini_set'
          4        SEND_VAL                                                 'display_errors'
          5        SEND_VAL                                                 1
          6        DO_ICALL                                                 
  105     7        DECLARE_CLASS                                            'myclass'
  123     8        NEW                                              $3      'MyClass'
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !0, $3
  124    11        INIT_METHOD_CALL                                         !0, 'setInstant'
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0  $6      
         14        INIT_METHOD_CALL                                         $6, 'getInstant'
         15        DO_FCALL                                      0  $7      
         16        ECHO                                                     $7
         17      > RETURN                                                   1

Class LazyGetSet:
Function setfluentsetter:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0264V
function name:  setFluentSetter
number of ops:  4
compiled vars:  !0 = $flag
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   16     1        ASSIGN_OBJ                                               'useFluentSetter'
          2        OP_DATA                                                  !0
   17     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/0264V
function name:  __call
number of ops:  37
compiled vars:  !0 = $method, !1 = $args, !2 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   21     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
   22    14    >   INIT_METHOD_CALL                                         'lazyGet'
         15        SEND_VAR_EX                                              !2
         16        SEND_VAR_EX                                              !1
         17        DO_FCALL                                      0  $7      
         18      > RETURN                                                   $7
   25    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
   26    31    >   INIT_METHOD_CALL                                         'lazySet'
         32        SEND_VAR_EX                                              !2
         33        SEND_VAR_EX                                              !1
         34        DO_FCALL                                      0  $12     
         35      > RETURN                                                   $12
   28    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/0264V
function name:  getLazyPropertyName
number of ops:  17
compiled vars:  !0 = $method, !1 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     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
   36     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
   37    15    > > RETURN                                                   !1
   39    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/0264V
function name:  lazyGet
number of ops:  4
compiled vars:  !0 = $ptyName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        FETCH_OBJ_R                                      ~1      !0
          2      > RETURN                                                   ~1
   47     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/0264V
function name:  lazySet
number of ops:  17
compiled vars:  !0 = $ptyName, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   56     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
   57     9    >   FETCH_DIM_R                                      ~5      !1, 0
         10        ASSIGN_OBJ                                               !0
         11        OP_DATA                                                  ~5
   60    12    >   FETCH_OBJ_R                                      ~6      'useFluentSetter'
         13      > JMPZ                                                     ~6, ->16
   61    14    >   FETCH_THIS                                       ~7      
         15      > RETURN                                                   ~7
   63    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/0264V
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
-------------------------------------------------------------------------------------
   65     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   66     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
   67     8        ISSET_ISEMPTY_DIM_OBJ                         0          !2, '%40var'
          9      > JMPZ                                                     ~7, ->28
   68    10    >   ASSIGN                                                   !3, <array>
   78    11        FETCH_DIM_R                                      ~9      !2, '%40var'
         12        ISSET_ISEMPTY_DIM_OBJ                         0          !3, ~9
         13      > JMPZ                                                     ~10, ->21
   79    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
   81    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
   84    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/0264V
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
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   87     2        NEW                                              $7      'ReflectionClass'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $7
   88     6        ASSIGN                                                   !3, <array>
   89     7        ASSIGN                                                   !4, <array>
   90     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
   91    24    >   INIT_FCALL                                               'trim'
         25        SEND_VAR                                                 !5
         26        DO_ICALL                                         $18     
         27        ASSIGN_DIM                                               !4
         28        OP_DATA                                                  $18
   90    29      > JMP                                                      ->23
         30    >   FE_FREE                                                  $16
   93    31        ASSIGN                                                   !6, <array>
   94    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
   95    37    >   ASSIGN                                                   !3, <array>
   96    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                                                 
   97    43      > JMPZ                                                     !3, ->51
   98    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
   94    51    > > JMP                                                      ->36
         52    >   FE_FREE                                                  $21
  101    53      > RETURN                                                   !6
  102    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:
172.21 ms | 1416 KiB | 37 Q