3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private int $age; private string $name; public function __construct () { $this->age = rand(12, 60); } } class FooList { private array $elementsList = []; public function setList (array $newList): void { foreach ($newList as $key => $value) { if (!$value instanceof Foo) { throw new Exception("GOVNYAK!!!"); } } $this->elementsList = $newList; } public function getList (): array { return $this->elementsList; } public function addToList (mixed $element): void { // Тут не используется TypeHint, потому что класс FooList на самом деле базовый, от которого наследуются другие. Я ведь не могу сделать что-то типа `addToList ($this->expectedClass $element): void`, проверять приходится в коде. Да и один хрен в коде придётся проверять для метода `setList()`, потому что там тип array, а условное Foo[] в качестве типа в PHP не завезли. if (!$element instanceof Foo) { throw new Exception("GOVNYAK!!!"); } $this->elementsList[] = $element; } public function removeFromList (int $index): void { $tmpList = $this->getList(); array_splice($tmpList, $index, 1); // Вот а если тут ещё какой-то код, который дополнительно преобразовывает массив каким-то образом? Не знаю, сортирует там. И проверяет, чтобы сумма $age всех элементов массива не была больше X и меньше Y. array_splice уже выполнен, и если мы будем выполнять его не над $tmpList, а над $this->elementsList, то элемент один хрен будет удалён. Хотя на самом деле метод должен выкинуть исключение и откатить данные к тому состоянию, которое было до его вызова. $this->setList($tmpList); } } $lst = new FooList(); $fooOne = new Foo(); $fooTwo = new Foo(); $govnyak = new stdClass(); $lst->addToList($fooOne); $lst->addToList($fooTwo); var_dump($lst); $lst->addToList($govnyak);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5UORL
function name:  (null)
number of ops:  25
compiled vars:  !0 = $lst, !1 = $fooOne, !2 = $fooTwo, !3 = $govnyak
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   NEW                                              $4      'FooList'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $4
   48     3        NEW                                              $7      'Foo'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !1, $7
   49     6        NEW                                              $10     'Foo'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $10
   50     9        NEW                                              $13     'stdClass'
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !3, $13
   52    12        INIT_METHOD_CALL                                         !0, 'addToList'
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0          
   53    15        INIT_METHOD_CALL                                         !0, 'addToList'
         16        SEND_VAR_EX                                              !2
         17        DO_FCALL                                      0          
   54    18        INIT_FCALL                                               'var_dump'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                                 
   55    21        INIT_METHOD_CALL                                         !0, 'addToList'
         22        SEND_VAR_EX                                              !3
         23        DO_FCALL                                      0          
         24      > RETURN                                                   1

Class Foo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5UORL
function name:  __construct
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   INIT_FCALL                                               'rand'
          1        SEND_VAL                                                 12
          2        SEND_VAL                                                 60
          3        DO_ICALL                                         $1      
          4        ASSIGN_OBJ                                               'age'
          5        OP_DATA                                                  $1
    9     6      > RETURN                                                   null

End of function __construct

End of class Foo.

Class FooList:
Function setlist:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 12
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/5UORL
function name:  setList
number of ops:  16
compiled vars:  !0 = $newList, !1 = $value, !2 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1      > FE_RESET_R                                       $3      !0, ->12
          2    > > FE_FETCH_R                                       ~4      $3, !1, ->12
          3    >   ASSIGN                                                   !2, ~4
   17     4        INSTANCEOF                                       ~6      !1, 'Foo'
          5        BOOL_NOT                                         ~7      ~6
          6      > JMPZ                                                     ~7, ->11
   18     7    >   NEW                                              $8      'Exception'
          8        SEND_VAL_EX                                              'GOVNYAK%21%21%21'
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $8
   16    11    > > JMP                                                      ->2
         12    >   FE_FREE                                                  $3
   22    13        ASSIGN_OBJ                                               'elementsList'
         14        OP_DATA                                                  !0
   23    15      > RETURN                                                   null

End of function setlist

Function getlist:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5UORL
function name:  getList
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   FETCH_OBJ_R                                      ~0      'elementsList'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   27     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getlist

Function addtolist:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5UORL
function name:  addToList
number of ops:  12
compiled vars:  !0 = $element
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   RECV                                             !0      
   31     1        INSTANCEOF                                       ~1      !0, 'Foo'
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->8
   32     4    >   NEW                                              $3      'Exception'
          5        SEND_VAL_EX                                              'GOVNYAK%21%21%21'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $3
   35     8    >   FETCH_OBJ_W                                      $5      'elementsList'
          9        ASSIGN_DIM                                               $5
         10        OP_DATA                                                  !0
   36    11      > RETURN                                                   null

End of function addtolist

Function removefromlist:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/5UORL
function name:  removeFromList
number of ops:  13
compiled vars:  !0 = $index, !1 = $tmpList
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
   39     1        INIT_METHOD_CALL                                         'getList'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !1, $2
   40     4        INIT_FCALL                                               'array_splice'
          5        SEND_REF                                                 !1
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 1
          8        DO_ICALL                                                 
   42     9        INIT_METHOD_CALL                                         'setList'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
   43    12      > RETURN                                                   null

End of function removefromlist

End of class FooList.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.83 ms | 1009 KiB | 16 Q