3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute] class Getter{} #[Attribute] class Setter{} class CallingInaccessibleUndeclaredMethod extends Exception{} class AccessingNonExistentProperty extends Exception{} class PassingInvalidNumberOfArguments extends Exception{} trait SetterGetter { private static array $listOfGettersAndSetters = []; private function getFieldNameFromMethodName(string $methodName): string { $fieldName = mb_substr($methodName, 3); $firstChar = mb_strtolower(mb_substr($fieldName, 0, 1)); $lastChars = mb_substr($fieldName, 1); $fieldName = $firstChar . $lastChars; return $fieldName; } private function getPropertyAccessMethods(string $methodName, object $object, string $fieldName): array { $methods = []; $property = new ReflectionProperty(get_class($object), $fieldName); $properties = $property->getAttributes(); foreach ($properties as $property) { $methodName = $property->getName(); if ($methodName === 'Setter') { $methods[] = 'set'; } elseif ($methodName === 'Getter') { $methods[] = 'get'; } } return $methods; } public function __call(string $name, array $arguments) { $prefix = mb_substr($name, 0, 3); if ($prefix !== 'get' && $prefix !== 'set') { throw new CallingInaccessibleUndeclaredMethod('Из необъявленных методов этого класса можно вызывать только геттер и сеттер.'); } $fieldName = $this->getFieldNameFromMethodName($name); if (!property_exists(get_class($this), $fieldName)) { throw new AccessingNonExistentProperty('В классе ' . get_class($object) . 'не существует свойства с именем' . $fieldName); } $fieldValue = $arguments[0] ?? null; if (isset(self::$listOfGettersAndSetters[$name]) && $prefix === 'get') { return self::$listOfGettersAndSetters[$name](); } elseif (isset(self::$listOfGettersAndSetters[$name]) && $prefix === 'set') { return self::$listOfGettersAndSetters[$name]($fieldValue); } else { $methods = $this->getPropertyAccessMethods($name, $this, $fieldName); if (!in_array($prefix, $methods)) { throw new CallingInaccessibleUndeclaredMethod('К этому полю нельзя обращаться через метод ' . $prefix); } if ($prefix === 'get') { if (count($arguments) !== 0) { throw new PassingInvalidNumberOfArguments('В геттер нельзя передавать аргументы.'); } self::$listOfGettersAndSetters[$name] = fn() => $this->$fieldName; return self::$listOfGettersAndSetters[$name](); } elseif ($prefix === 'set') { if (count($arguments) !== 1) { throw new PassingInvalidNumberOfArguments('В сеттер можно передавать только один аргумент.'); } self::$listOfGettersAndSetters[$name] = fn($fieldValue) => $this->$fieldName = $fieldValue; self::$listOfGettersAndSetters[$name]($fieldValue); } } } } class Test { use SetterGetter; #[Setter] #[Getter] private int $number; } $test = new Test(); $test->setNumber(5); echo $test->getNumber();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Jfpr9
function name:  (null)
number of ops:  11
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   87     0  E >   DECLARE_CLASS                                            'test'
   96     1        NEW                                              $1      'Test'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   97     4        INIT_METHOD_CALL                                         !0, 'setNumber'
          5        SEND_VAL_EX                                              5
          6        DO_FCALL                                      0          
   98     7        INIT_METHOD_CALL                                         !0, 'getNumber'
          8        DO_FCALL                                      0  $5      
          9        ECHO                                                     $5
         10      > RETURN                                                   1

Class Getter: [no user functions]
Class Setter: [no user functions]
Class CallingInaccessibleUndeclaredMethod: [no user functions]
Class AccessingNonExistentProperty: [no user functions]
Class PassingInvalidNumberOfArguments: [no user functions]
Class SetterGetter:
Function getfieldnamefrommethodname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Jfpr9
function name:  getFieldNameFromMethodName
number of ops:  26
compiled vars:  !0 = $methodName, !1 = $fieldName, !2 = $firstChar, !3 = $lastChars
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
   18     1        INIT_FCALL                                               'mb_substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 3
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   19     6        INIT_FCALL                                               'mb_strtolower'
          7        INIT_FCALL                                               'mb_substr'
          8        SEND_VAR                                                 !1
          9        SEND_VAL                                                 0
         10        SEND_VAL                                                 1
         11        DO_ICALL                                         $6      
         12        SEND_VAR                                                 $6
         13        DO_ICALL                                         $7      
         14        ASSIGN                                                   !2, $7
   20    15        INIT_FCALL                                               'mb_substr'
         16        SEND_VAR                                                 !1
         17        SEND_VAL                                                 1
         18        DO_ICALL                                         $9      
         19        ASSIGN                                                   !3, $9
   21    20        CONCAT                                           ~11     !2, !3
         21        ASSIGN                                                   !1, ~11
   22    22        VERIFY_RETURN_TYPE                                       !1
         23      > RETURN                                                   !1
   23    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function getfieldnamefrommethodname

Function getpropertyaccessmethods:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 28
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 28
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 23
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 27
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/Jfpr9
function name:  getPropertyAccessMethods
number of ops:  33
compiled vars:  !0 = $methodName, !1 = $object, !2 = $fieldName, !3 = $methods, !4 = $property, !5 = $properties
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   27     3        ASSIGN                                                   !3, <array>
   28     4        NEW                                              $7      'ReflectionProperty'
          5        GET_CLASS                                        ~8      !1
          6        SEND_VAL_EX                                              ~8
          7        SEND_VAR_EX                                              !2
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !4, $7
   29    10        INIT_METHOD_CALL                                         !4, 'getAttributes'
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !5, $11
   30    13      > FE_RESET_R                                       $13     !5, ->28
         14    > > FE_FETCH_R                                               $13, !4, ->28
   31    15    >   INIT_METHOD_CALL                                         !4, 'getName'
         16        DO_FCALL                                      0  $14     
         17        ASSIGN                                                   !0, $14
   32    18        IS_IDENTICAL                                             !0, 'Setter'
         19      > JMPZ                                                     ~16, ->23
   33    20    >   ASSIGN_DIM                                               !3
         21        OP_DATA                                                  'set'
   32    22      > JMP                                                      ->27
   34    23    >   IS_IDENTICAL                                             !0, 'Getter'
         24      > JMPZ                                                     ~18, ->27
   35    25    >   ASSIGN_DIM                                               !3
         26        OP_DATA                                                  'get'
   30    27    > > JMP                                                      ->14
         28    >   FE_FREE                                                  $13
   38    29        VERIFY_RETURN_TYPE                                       !3
         30      > RETURN                                                   !3
   39    31*       VERIFY_RETURN_TYPE                                       
         32*     > RETURN                                                   null

End of function getpropertyaccessmethods

Function __call:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 17
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 37
Branch analysis from position: 29
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 37
2 jumps found. (Code = 46) Position 1 = 44, Position 2 = 46
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 53
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
2 jumps found. (Code = 46) Position 1 = 56, Position 2 = 58
Branch analysis from position: 56
2 jumps found. (Code = 43) Position 1 = 59, Position 2 = 66
Branch analysis from position: 59
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 84
Branch analysis from position: 79
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 84
2 jumps found. (Code = 43) Position 1 = 86, Position 2 = 104
Branch analysis from position: 86
2 jumps found. (Code = 43) Position 1 = 89, Position 2 = 93
Branch analysis from position: 89
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 93
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 104
2 jumps found. (Code = 43) Position 1 = 106, Position 2 = 123
Branch analysis from position: 106
2 jumps found. (Code = 43) Position 1 = 109, Position 2 = 113
Branch analysis from position: 109
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 113
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 123
Branch analysis from position: 58
Branch analysis from position: 46
Branch analysis from position: 12
filename:       /in/Jfpr9
function name:  __call
number of ops:  124
compiled vars:  !0 = $name, !1 = $arguments, !2 = $prefix, !3 = $fieldName, !4 = $object, !5 = $fieldValue, !6 = $methods
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   43     2        INIT_FCALL                                               'mb_substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAL                                                 3
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !2, $7
   45     8        IS_NOT_IDENTICAL                                 ~9      !2, 'get'
          9      > JMPZ_EX                                          ~9      ~9, ->12
         10    >   IS_NOT_IDENTICAL                                 ~10     !2, 'set'
         11        BOOL                                             ~9      ~10
         12    > > JMPZ                                                     ~9, ->17
   46    13    >   NEW                                              $11     'CallingInaccessibleUndeclaredMethod'
         14        SEND_VAL_EX                                              '%D0%98%D0%B7+%D0%BD%D0%B5%D0%BE%D0%B1%D1%8A%D1%8F%D0%B2%D0%BB%D0%B5%D0%BD%D0%BD%D1%8B%D1%85+%D0%BC%D0%B5%D1%82%D0%BE%D0%B4%D0%BE%D0%B2+%D1%8D%D1%82%D0%BE%D0%B3%D0%BE+%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%B0+%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE+%D0%B2%D1%8B%D0%B7%D1%8B%D0%B2%D0%B0%D1%82%D1%8C+%D1%82%D0%BE%D0%BB%D1%8C%D0%BA%D0%BE+%D0%B3%D0%B5%D1%82%D1%82%D0%B5%D1%80+%D0%B8+%D1%81%D0%B5%D1%82%D1%82%D0%B5%D1%80.'
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $11
   49    17    >   INIT_METHOD_CALL                                         'getFieldNameFromMethodName'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $13     
         20        ASSIGN                                                   !3, $13
   51    21        INIT_FCALL                                               'property_exists'
         22        FETCH_THIS                                       ~15     
         23        GET_CLASS                                        ~16     ~15
         24        SEND_VAL                                                 ~16
         25        SEND_VAR                                                 !3
         26        DO_ICALL                                         $17     
         27        BOOL_NOT                                         ~18     $17
         28      > JMPZ                                                     ~18, ->37
   52    29    >   NEW                                              $19     'AccessingNonExistentProperty'
         30        GET_CLASS                                        ~20     !4
         31        CONCAT                                           ~21     '%D0%92+%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%B5+', ~20
         32        CONCAT                                           ~22     ~21, '%D0%BD%D0%B5+%D1%81%D1%83%D1%89%D0%B5%D1%81%D1%82%D0%B2%D1%83%D0%B5%D1%82+%D1%81%D0%B2%D0%BE%D0%B9%D1%81%D1%82%D0%B2%D0%B0+%D1%81+%D0%B8%D0%BC%D0%B5%D0%BD%D0%B5%D0%BC'
         33        CONCAT                                           ~23     ~22, !3
         34        SEND_VAL_EX                                              ~23
         35        DO_FCALL                                      0          
         36      > THROW                                         0          $19
   55    37    >   FETCH_DIM_IS                                     ~25     !1, 0
         38        COALESCE                                         ~26     ~25
         39        QM_ASSIGN                                        ~26     null
         40        ASSIGN                                                   !5, ~26
   57    41        FETCH_STATIC_PROP_IS                             ~28     'listOfGettersAndSetters'
         42        ISSET_ISEMPTY_DIM_OBJ                         0  ~29     ~28, !0
         43      > JMPZ_EX                                          ~29     ~29, ->46
         44    >   IS_IDENTICAL                                     ~30     !2, 'get'
         45        BOOL                                             ~29     ~30
         46    > > JMPZ                                                     ~29, ->53
   58    47    >   FETCH_STATIC_PROP_R          unknown             ~31     'listOfGettersAndSetters'
         48        FETCH_DIM_R                                      ~32     ~31, !0
         49        INIT_DYNAMIC_CALL                                        ~32
         50        DO_FCALL                                      0  $33     
         51      > RETURN                                                   $33
   57    52*       JMP                                                      ->123
   59    53    >   FETCH_STATIC_PROP_IS                             ~34     'listOfGettersAndSetters'
         54        ISSET_ISEMPTY_DIM_OBJ                         0  ~35     ~34, !0
         55      > JMPZ_EX                                          ~35     ~35, ->58
         56    >   IS_IDENTICAL                                     ~36     !2, 'set'
         57        BOOL                                             ~35     ~36
         58    > > JMPZ                                                     ~35, ->66
   60    59    >   FETCH_STATIC_PROP_R          unknown             ~37     'listOfGettersAndSetters'
         60        FETCH_DIM_R                                      ~38     ~37, !0
         61        INIT_DYNAMIC_CALL                                        ~38
         62        SEND_VAR_EX                                              !5
         63        DO_FCALL                                      0  $39     
         64      > RETURN                                                   $39
   59    65*       JMP                                                      ->123
   63    66    >   INIT_METHOD_CALL                                         'getPropertyAccessMethods'
         67        SEND_VAR_EX                                              !0
         68        FETCH_THIS                                       $40     
         69        SEND_VAR_EX                                              $40
         70        SEND_VAR_EX                                              !3
         71        DO_FCALL                                      0  $41     
         72        ASSIGN                                                   !6, $41
   65    73        INIT_FCALL                                               'in_array'
         74        SEND_VAR                                                 !2
         75        SEND_VAR                                                 !6
         76        DO_ICALL                                         $43     
         77        BOOL_NOT                                         ~44     $43
         78      > JMPZ                                                     ~44, ->84
   66    79    >   NEW                                              $45     'CallingInaccessibleUndeclaredMethod'
         80        CONCAT                                           ~46     '%D0%9A+%D1%8D%D1%82%D0%BE%D0%BC%D1%83+%D0%BF%D0%BE%D0%BB%D1%8E+%D0%BD%D0%B5%D0%BB%D1%8C%D0%B7%D1%8F+%D0%BE%D0%B1%D1%80%D0%B0%D1%89%D0%B0%D1%82%D1%8C%D1%81%D1%8F+%D1%87%D0%B5%D1%80%D0%B5%D0%B7+%D0%BC%D0%B5%D1%82%D0%BE%D0%B4+', !2
         81        SEND_VAL_EX                                              ~46
         82        DO_FCALL                                      0          
         83      > THROW                                         0          $45
   69    84    >   IS_IDENTICAL                                             !2, 'get'
         85      > JMPZ                                                     ~48, ->104
   70    86    >   COUNT                                            ~49     !1
         87        IS_NOT_IDENTICAL                                         ~49, 0
         88      > JMPZ                                                     ~50, ->93
   71    89    >   NEW                                              $51     'PassingInvalidNumberOfArguments'
         90        SEND_VAL_EX                                              '%D0%92+%D0%B3%D0%B5%D1%82%D1%82%D0%B5%D1%80+%D0%BD%D0%B5%D0%BB%D1%8C%D0%B7%D1%8F+%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D0%B2%D0%B0%D1%82%D1%8C+%D0%B0%D1%80%D0%B3%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D1%8B.'
         91        DO_FCALL                                      0          
         92      > THROW                                         0          $51
   73    93    >   DECLARE_LAMBDA_FUNCTION                          ~55     [0]
         94        BIND_LEXICAL                                             ~55, !3
         95        FETCH_STATIC_PROP_W          unknown             $53     'listOfGettersAndSetters'
         96        ASSIGN_DIM                                               $53, !0
         97        OP_DATA                                                  ~55
   74    98        FETCH_STATIC_PROP_R          unknown             ~56     'listOfGettersAndSetters'
         99        FETCH_DIM_R                                      ~57     ~56, !0
        100        INIT_DYNAMIC_CALL                                        ~57
        101        DO_FCALL                                      0  $58     
        102      > RETURN                                                   $58
   69   103*       JMP                                                      ->123
   75   104    >   IS_IDENTICAL                                             !2, 'set'
        105      > JMPZ                                                     ~59, ->123
   76   106    >   COUNT                                            ~60     !1
        107        IS_NOT_IDENTICAL                                         ~60, 1
        108      > JMPZ                                                     ~61, ->113
   77   109    >   NEW                                              $62     'PassingInvalidNumberOfArguments'
        110        SEND_VAL_EX                                              '%D0%92+%D1%81%D0%B5%D1%82%D1%82%D0%B5%D1%80+%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE+%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D0%B2%D0%B0%D1%82%D1%8C+%D1%82%D0%BE%D0%BB%D1%8C%D0%BA%D0%BE+%D0%BE%D0%B4%D0%B8%D0%BD+%D0%B0%D1%80%D0%B3%D1%83%D0%BC%D0%B5%D0%BD%D1%82.'
        111        DO_FCALL                                      0          
        112      > THROW                                         0          $62
   80   113    >   DECLARE_LAMBDA_FUNCTION                          ~66     [1]
        114        BIND_LEXICAL                                             ~66, !3
        115        FETCH_STATIC_PROP_W          unknown             $64     'listOfGettersAndSetters'
        116        ASSIGN_DIM                                               $64, !0
        117        OP_DATA                                                  ~66
   81   118        FETCH_STATIC_PROP_R          unknown             ~67     'listOfGettersAndSetters'
        119        FETCH_DIM_R                                      ~68     ~67, !0
        120        INIT_DYNAMIC_CALL                                        ~68
        121        SEND_VAR_EX                                              !5
        122        DO_FCALL                                      0          
   84   123    > > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Jfpr9
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $fieldName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   BIND_STATIC                                              !0
          1        FETCH_THIS                                       $1      
          2        FETCH_OBJ_R                                      ~2      $1, !0
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Jfpr9
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $fieldValue, !1 = $fieldName
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_THIS                                       $2      
          3        ASSIGN_OBJ                                       ~3      $2, !1
          4        OP_DATA                                                  !0
          5      > RETURN                                                   ~3
          6*     > RETURN                                                   null

End of Dynamic Function 1

End of function __call

End of class SetterGetter.

Class Test: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.84 ms | 1022 KiB | 17 Q