3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute] class Getter{} #[Attribute] class Setter{} class SetterGetterException extends Exception{} trait SetterGetter { private function getFieldNameFromMethodName(string $methodName): string { $fieldName = mb_substr($methodName, 3); $fieldNameFirstChar = mb_strtolower(mb_substr($fieldName, 0, 1)); $fieldNameLastChars = mb_substr($fieldName, 1); $fieldName = $fieldNameFirstChar . $fieldNameLastChars; return $fieldName; } private function methodAllowedToBeUsedOnProperty(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 SetterGetterException('Из необъявленных методов этого класса можно вызывать только геттер и сеттер.'); } $fieldName = $this->getFieldNameFromMethodName($name); if (!property_exists(get_class($this), $fieldName)) { throw new SetterGetterException('В классе ' . get_class($object) . 'не существует свойства с именем' . $fieldName); } $methods = $this->methodAllowedToBeUsedOnProperty($name, $this, $fieldName); if (!in_array($prefix, $methods)) { throw new SetterGetterException('К этому полю нельзя обращаться через метод ' . $prefix); } if ($prefix === 'get') { return $this->$fieldName; } elseif ($prefix === 'set') { if (count($arguments) !== 1) { throw new SetterGetterException('В сеттер можно передавать только один аргумент.'); } $fieldValue = $arguments[0]; $this->$fieldName = $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/NpEpS
function name:  (null)
number of ops:  11
compiled vars:  !0 = $test
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   DECLARE_CLASS                                            'test'
   80     1        NEW                                              $1      'Test'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   81     4        INIT_METHOD_CALL                                         !0, 'setNumber'
          5        SEND_VAL_EX                                              5
          6        DO_FCALL                                      0          
   82     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 SetterGetterException: [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/NpEpS
function name:  getFieldNameFromMethodName
number of ops:  26
compiled vars:  !0 = $methodName, !1 = $fieldName, !2 = $fieldNameFirstChar, !3 = $fieldNameLastChars
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'mb_substr'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 3
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
   15     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
   16    15        INIT_FCALL                                               'mb_substr'
         16        SEND_VAR                                                 !1
         17        SEND_VAL                                                 1
         18        DO_ICALL                                         $9      
         19        ASSIGN                                                   !3, $9
   17    20        CONCAT                                           ~11     !2, !3
         21        ASSIGN                                                   !1, ~11
   18    22        VERIFY_RETURN_TYPE                                       !1
         23      > RETURN                                                   !1
   19    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function getfieldnamefrommethodname

Function methodallowedtobeusedonproperty:
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/NpEpS
function name:  methodAllowedToBeUsedOnProperty
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
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   23     3        ASSIGN                                                   !3, <array>
   24     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
   25    10        INIT_METHOD_CALL                                         !4, 'getAttributes'
         11        DO_FCALL                                      0  $11     
         12        ASSIGN                                                   !5, $11
   26    13      > FE_RESET_R                                       $13     !5, ->28
         14    > > FE_FETCH_R                                               $13, !4, ->28
   27    15    >   INIT_METHOD_CALL                                         !4, 'getName'
         16        DO_FCALL                                      0  $14     
         17        ASSIGN                                                   !0, $14
   28    18        IS_IDENTICAL                                             !0, 'Setter'
         19      > JMPZ                                                     ~16, ->23
   29    20    >   ASSIGN_DIM                                               !3
         21        OP_DATA                                                  'set'
   28    22      > JMP                                                      ->27
   30    23    >   IS_IDENTICAL                                             !0, 'Getter'
         24      > JMPZ                                                     ~18, ->27
   31    25    >   ASSIGN_DIM                                               !3
         26        OP_DATA                                                  'get'
   26    27    > > JMP                                                      ->14
         28    >   FE_FREE                                                  $13
   34    29        VERIFY_RETURN_TYPE                                       !3
         30      > RETURN                                                   !3
   35    31*       VERIFY_RETURN_TYPE                                       
         32*     > RETURN                                                   null

End of function methodallowedtobeusedonproperty

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 = 43) Position 1 = 50, Position 2 = 55
Branch analysis from position: 50
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 57, Position 2 = 60
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
2 jumps found. (Code = 43) Position 1 = 62, Position 2 = 73
Branch analysis from position: 62
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 69
Branch analysis from position: 65
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 69
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 73
Branch analysis from position: 12
filename:       /in/NpEpS
function name:  __call
number of ops:  74
compiled vars:  !0 = $name, !1 = $arguments, !2 = $prefix, !3 = $fieldName, !4 = $object, !5 = $methods, !6 = $fieldValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   39     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
   41     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
   42    13    >   NEW                                              $11     'SetterGetterException'
         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
   45    17    >   INIT_METHOD_CALL                                         'getFieldNameFromMethodName'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0  $13     
         20        ASSIGN                                                   !3, $13
   47    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
   48    29    >   NEW                                              $19     'SetterGetterException'
         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
   51    37    >   INIT_METHOD_CALL                                         'methodAllowedToBeUsedOnProperty'
         38        SEND_VAR_EX                                              !0
         39        FETCH_THIS                                       $25     
         40        SEND_VAR_EX                                              $25
         41        SEND_VAR_EX                                              !3
         42        DO_FCALL                                      0  $26     
         43        ASSIGN                                                   !5, $26
   53    44        INIT_FCALL                                               'in_array'
         45        SEND_VAR                                                 !2
         46        SEND_VAR                                                 !5
         47        DO_ICALL                                         $28     
         48        BOOL_NOT                                         ~29     $28
         49      > JMPZ                                                     ~29, ->55
   54    50    >   NEW                                              $30     'SetterGetterException'
         51        CONCAT                                           ~31     '%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
         52        SEND_VAL_EX                                              ~31
         53        DO_FCALL                                      0          
         54      > THROW                                         0          $30
   57    55    >   IS_IDENTICAL                                             !2, 'get'
         56      > JMPZ                                                     ~33, ->60
   58    57    >   FETCH_OBJ_R                                      ~34     !3
         58      > RETURN                                                   ~34
   57    59*       JMP                                                      ->73
   59    60    >   IS_IDENTICAL                                             !2, 'set'
         61      > JMPZ                                                     ~35, ->73
   60    62    >   COUNT                                            ~36     !1
         63        IS_NOT_IDENTICAL                                         ~36, 1
         64      > JMPZ                                                     ~37, ->69
   61    65    >   NEW                                              $38     'SetterGetterException'
         66        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.'
         67        DO_FCALL                                      0          
         68      > THROW                                         0          $38
   64    69    >   FETCH_DIM_R                                      ~40     !1, 0
         70        ASSIGN                                                   !6, ~40
   66    71        ASSIGN_OBJ                                               !3
         72        OP_DATA                                                  !6
   68    73    > > RETURN                                                   null

End of function __call

End of class SetterGetter.

Class Test: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
137.66 ms | 1018 KiB | 17 Q