3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ExpressionCalculator { private $operators = array(); public function __construct() { $operators['+'] = function ($leftOperand, $rightOperand) { return $leftOperand + $rightOperand; }; $operators['-'] = function ($leftOperand, $rightOperand) { return $leftOperand - $rightOperand; }; $operators['*'] = function ($leftOperand, $rightOperand) { return $leftOperand * $rightOperand; }; $operators['/'] = function ($leftOperand, $rightOperand) { return $leftOperand / $rightOperand; }; } public function calc($expression) { $operands = array(); $i = 0; while (($token = strtok($expression, ' ')) !== false) { echo $token; $i++; if ($i > 100) { die; } if ($this->isOperator($token)) { $rightOperand = array_pop($operands); $leftOperand = array_pop($operands); $result = $this->execute($token, $leftOperand, $rightOperand); array_push($operands, $result); } else if (is_numeric($token)) { array_push($operands, $token); } else { throw new InvalidArgumentException('Expression has invalid token: ' + $token); } } return $result; } private function isOperator($token) { return isset($operators[$token]); } private function execute($operator, $leftOperand, $rightOperand) { return call_user_func($operators[$operator], $leftOperand, $rightOperand); } } $calc = new ExpressionCalculator(); echo $calc->calc('5 1 2 + 4 * + 3 -'); // 14
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  (null)
number of ops:  8
compiled vars:  !0 = $calc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   NEW                                              $1      'ExpressionCalculator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   58     3        INIT_METHOD_CALL                                         !0, 'calc'
          4        SEND_VAL_EX                                              '5+1+2+%2B+4+%2A+%2B+3+-'
          5        DO_FCALL                                      0  $4      
          6        ECHO                                                     $4
          7      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FZBqoe%3A7%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    8     2        ADD                                              ~2      !0, !1
          3      > RETURN                                                   ~2
    9     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FZBqoe%3A7%240

Function %00%7Bclosure%7D%2Fin%2FZBqoe%3A10%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        SUB                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   12     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FZBqoe%3A10%241

Function %00%7Bclosure%7D%2Fin%2FZBqoe%3A13%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        MUL                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   15     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FZBqoe%3A13%242

Function %00%7Bclosure%7D%2Fin%2FZBqoe%3A16%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $leftOperand, !1 = $rightOperand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   17     2        DIV                                              ~2      !0, !1
          3      > RETURN                                                   ~2
   18     4*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FZBqoe%3A16%243

Class ExpressionCalculator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  __construct
number of ops:  13
compiled vars:  !0 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FZBqoe%3A7%240'
          1        ASSIGN_DIM                                               !0, '%2B'
    9     2        OP_DATA                                                  ~2
   10     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FZBqoe%3A10%241'
          4        ASSIGN_DIM                                               !0, '-'
   12     5        OP_DATA                                                  ~4
   13     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FZBqoe%3A13%242'
          7        ASSIGN_DIM                                               !0, '%2A'
   15     8        OP_DATA                                                  ~6
   16     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FZBqoe%3A16%243'
         10        ASSIGN_DIM                                               !0, '%2F'
   18    11        OP_DATA                                                  ~8
   19    12      > RETURN                                                   null

End of function __construct

Function calc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 4
Branch analysis from position: 53
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 79) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 32
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 36, Position 2 = 41
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 41
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/ZBqoe
function name:  calc
number of ops:  55
compiled vars:  !0 = $expression, !1 = $operands, !2 = $i, !3 = $token, !4 = $rightOperand, !5 = $leftOperand, !6 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        ASSIGN                                                   !1, <array>
   23     2        ASSIGN                                                   !2, 0
   25     3      > JMP                                                      ->46
   26     4    >   ECHO                                                     !3
   27     5        PRE_INC                                                  !2
   29     6        IS_SMALLER                                               100, !2
          7      > JMPZ                                                     ~10, ->9
   30     8    > > EXIT                                                     
   33     9    >   INIT_METHOD_CALL                                         'isOperator'
         10        SEND_VAR_EX                                              !3
         11        DO_FCALL                                      0  $11     
         12      > JMPZ                                                     $11, ->32
   34    13    >   INIT_FCALL                                               'array_pop'
         14        SEND_REF                                                 !1
         15        DO_ICALL                                         $12     
         16        ASSIGN                                                   !4, $12
   35    17        INIT_FCALL                                               'array_pop'
         18        SEND_REF                                                 !1
         19        DO_ICALL                                         $14     
         20        ASSIGN                                                   !5, $14
   36    21        INIT_METHOD_CALL                                         'execute'
         22        SEND_VAR_EX                                              !3
         23        SEND_VAR_EX                                              !5
         24        SEND_VAR_EX                                              !4
         25        DO_FCALL                                      0  $16     
         26        ASSIGN                                                   !6, $16
   37    27        INIT_FCALL                                               'array_push'
         28        SEND_REF                                                 !1
         29        SEND_VAR                                                 !6
         30        DO_ICALL                                                 
         31      > JMP                                                      ->46
   38    32    >   INIT_FCALL                                               'is_numeric'
         33        SEND_VAR                                                 !3
         34        DO_ICALL                                         $19     
         35      > JMPZ                                                     $19, ->41
   39    36    >   INIT_FCALL                                               'array_push'
         37        SEND_REF                                                 !1
         38        SEND_VAR                                                 !3
         39        DO_ICALL                                                 
         40      > JMP                                                      ->46
   41    41    >   NEW                                              $21     'InvalidArgumentException'
         42        ADD                                              ~22     'Expression+has+invalid+token%3A+', !3
         43        SEND_VAL_EX                                              ~22
         44        DO_FCALL                                      0          
         45      > THROW                                         0          $21
   25    46    >   INIT_FCALL                                               'strtok'
         47        SEND_VAR                                                 !0
         48        SEND_VAL                                                 '+'
         49        DO_ICALL                                         $24     
         50        ASSIGN                                           ~25     !3, $24
         51        TYPE_CHECK                                  1018          ~25
         52      > JMPNZ                                                    ~26, ->4
   45    53    > > RETURN                                                   !6
   46    54*     > RETURN                                                   null

End of function calc

Function isoperator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  isOperator
number of ops:  4
compiled vars:  !0 = $token, !1 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      !1, !0
          2      > RETURN                                                   ~2
   50     3*     > RETURN                                                   null

End of function isoperator

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZBqoe
function name:  execute
number of ops:  10
compiled vars:  !0 = $operator, !1 = $leftOperand, !2 = $rightOperand, !3 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   53     3        FETCH_DIM_R                                      ~4      !3, !0
          4        INIT_USER_CALL                                2          'call_user_func', ~4
          5        SEND_USER                                                !1
          6        SEND_USER                                                !2
          7        DO_FCALL                                      0  $5      
          8      > RETURN                                                   $5
   54     9*     > RETURN                                                   null

End of function execute

End of class ExpressionCalculator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
271.14 ms | 1408 KiB | 22 Q