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(); while (($token = strtok($expression, ' ')) !== false) { echo $token; 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/qkCGK
function name:  (null)
number of ops:  8
compiled vars:  !0 = $calc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   NEW                                              $1      'ExpressionCalculator'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   51     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%2FqkCGK%3A7%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qkCGK
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%2FqkCGK%3A7%240

Function %00%7Bclosure%7D%2Fin%2FqkCGK%3A10%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qkCGK
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%2FqkCGK%3A10%241

Function %00%7Bclosure%7D%2Fin%2FqkCGK%3A13%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qkCGK
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%2FqkCGK%3A13%242

Function %00%7Bclosure%7D%2Fin%2FqkCGK%3A16%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qkCGK
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%2FqkCGK%3A16%243

Class ExpressionCalculator:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qkCGK
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%2FqkCGK%3A7%240'
          1        ASSIGN_DIM                                               !0, '%2B'
    9     2        OP_DATA                                                  ~2
   10     3        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FqkCGK%3A10%241'
          4        ASSIGN_DIM                                               !0, '-'
   12     5        OP_DATA                                                  ~4
   13     6        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FqkCGK%3A13%242'
          7        ASSIGN_DIM                                               !0, '%2A'
   15     8        OP_DATA                                                  ~6
   16     9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FqkCGK%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 = 41
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 3
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 27
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 31, Position 2 = 36
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 36
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/qkCGK
function name:  calc
number of ops:  50
compiled vars:  !0 = $expression, !1 = $operands, !2 = $token, !3 = $rightOperand, !4 = $leftOperand, !5 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   22     1        ASSIGN                                                   !1, <array>
   24     2      > JMP                                                      ->41
   25     3    >   ECHO                                                     !2
   26     4        INIT_METHOD_CALL                                         'isOperator'
          5        SEND_VAR_EX                                              !2
          6        DO_FCALL                                      0  $7      
          7      > JMPZ                                                     $7, ->27
   27     8    >   INIT_FCALL                                               'array_pop'
          9        SEND_REF                                                 !1
         10        DO_ICALL                                         $8      
         11        ASSIGN                                                   !3, $8
   28    12        INIT_FCALL                                               'array_pop'
         13        SEND_REF                                                 !1
         14        DO_ICALL                                         $10     
         15        ASSIGN                                                   !4, $10
   29    16        INIT_METHOD_CALL                                         'execute'
         17        SEND_VAR_EX                                              !2
         18        SEND_VAR_EX                                              !4
         19        SEND_VAR_EX                                              !3
         20        DO_FCALL                                      0  $12     
         21        ASSIGN                                                   !5, $12
   30    22        INIT_FCALL                                               'array_push'
         23        SEND_REF                                                 !1
         24        SEND_VAR                                                 !5
         25        DO_ICALL                                                 
         26      > JMP                                                      ->41
   31    27    >   INIT_FCALL                                               'is_numeric'
         28        SEND_VAR                                                 !2
         29        DO_ICALL                                         $15     
         30      > JMPZ                                                     $15, ->36
   32    31    >   INIT_FCALL                                               'array_push'
         32        SEND_REF                                                 !1
         33        SEND_VAR                                                 !2
         34        DO_ICALL                                                 
         35      > JMP                                                      ->41
   34    36    >   NEW                                              $17     'InvalidArgumentException'
         37        ADD                                              ~18     'Expression+has+invalid+token%3A+', !2
         38        SEND_VAL_EX                                              ~18
         39        DO_FCALL                                      0          
         40      > THROW                                         0          $17
   24    41    >   INIT_FCALL                                               'strtok'
         42        SEND_VAR                                                 !0
         43        SEND_VAL                                                 '+'
         44        DO_ICALL                                         $20     
         45        ASSIGN                                           ~21     !2, $20
         46        TYPE_CHECK                                  1018          ~21
         47      > JMPNZ                                                    ~22, ->3
   38    48    > > RETURN                                                   !5
   39    49*     > 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/qkCGK
function name:  isOperator
number of ops:  4
compiled vars:  !0 = $token, !1 = $operators
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   42     1        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      !1, !0
          2      > RETURN                                                   ~2
   43     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/qkCGK
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
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   46     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
   47     9*     > RETURN                                                   null

End of function execute

End of class ExpressionCalculator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.73 ms | 1408 KiB | 21 Q