3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Psuedo Model class Model { protected $comments; public function __construct() { $this->comments = new class { public function count() { $responses = [ 1, 2, 3, "Error: could not connect to database", 5 ]; $rand = rand(0, 4); if (is_string($responses[$rand])) { throw new Exception($responses[$rand]); } return $responses[$rand]; } }; } } // Log our errors and exceptions somewhere. class Logger { public static function log(string $log): void { echo $log; } } // Comment Count value object. final class CommentCount { private $count; public function __construct(int $count) { $this->count = $count; } public function getCount(): int { return $this->count; } } // Our core business logic to work out if we've had a lot of comments or not. class CommentWow { public function wow(CommentCount $commentCount): string { if ($commentCount->getCount() == 0) { return "Sad. :("; } if ($commentCount->getCount() > 0 && $commentCount->getCount() < 5) { return "Ok."; } if ($commentCount->getCount() >= 5) { return "Wow! :)"; } } } // Post model to retrieve comments count for a post. class Post extends Model { public function commentCount(): CommentCount { try { return new CommentCount($this->comments->count()); } catch (Exception $e) { Logger::log($e->getMessage()); return new CommentCount(0); } } } $post = new Post(); $commentCount = $post->commentCount(); $wow = new CommentWow(); var_dump($wow->wow($commentCount)); var_dump($wow->wow($commentCount)); var_dump($wow->wow($commentCount)); var_dump($wow->wow($commentCount)); var_dump($wow->wow($commentCount));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  (null)
number of ops:  40
compiled vars:  !0 = $post, !1 = $commentCount, !2 = $wow
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   92     0  E >   NEW                                              $3      'Post'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $3
   93     3        INIT_METHOD_CALL                                         !0, 'commentCount'
          4        DO_FCALL                                      0  $6      
          5        ASSIGN                                                   !1, $6
   95     6        NEW                                              $8      'CommentWow'
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !2, $8
   97     9        INIT_FCALL                                               'var_dump'
         10        INIT_METHOD_CALL                                         !2, 'wow'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0  $11     
         13        SEND_VAR                                                 $11
         14        DO_ICALL                                                 
   98    15        INIT_FCALL                                               'var_dump'
         16        INIT_METHOD_CALL                                         !2, 'wow'
         17        SEND_VAR_EX                                              !1
         18        DO_FCALL                                      0  $13     
         19        SEND_VAR                                                 $13
         20        DO_ICALL                                                 
   99    21        INIT_FCALL                                               'var_dump'
         22        INIT_METHOD_CALL                                         !2, 'wow'
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0  $15     
         25        SEND_VAR                                                 $15
         26        DO_ICALL                                                 
  100    27        INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !2, 'wow'
         29        SEND_VAR_EX                                              !1
         30        DO_FCALL                                      0  $17     
         31        SEND_VAR                                                 $17
         32        DO_ICALL                                                 
  101    33        INIT_FCALL                                               'var_dump'
         34        INIT_METHOD_CALL                                         !2, 'wow'
         35        SEND_VAR_EX                                              !1
         36        DO_FCALL                                      0  $19     
         37        SEND_VAR                                                 $19
         38        DO_ICALL                                                 
         39      > RETURN                                                   1

Class class@anonymous:
Function count:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  count
number of ops:  18
compiled vars:  !0 = $responses, !1 = $rand
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   ASSIGN                                                   !0, <array>
   21     1        INIT_FCALL                                               'rand'
          2        SEND_VAL                                                 0
          3        SEND_VAL                                                 4
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   23     6        FETCH_DIM_R                                      ~5      !0, !1
          7        TYPE_CHECK                                   64          ~5
          8      > JMPZ                                                     ~6, ->15
   24     9    >   NEW                                              $7      'Exception'
         10        CHECK_FUNC_ARG                                           
         11        FETCH_DIM_FUNC_ARG                               $8      !0, !1
         12        SEND_FUNC_ARG                                            $8
         13        DO_FCALL                                      0          
         14      > THROW                                         0          $7
   27    15    >   FETCH_DIM_R                                      ~10     !0, !1
         16      > RETURN                                                   ~10
   28    17*     > RETURN                                                   null

End of function count

End of class class@anonymous.

Class Model:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  __construct
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_ANON_CLASS                               <unknown> 
          1        NEW                                              $2      $1
          2        DO_FCALL                                      0          
          3        ASSIGN_OBJ                                               'comments'
          4        OP_DATA                                                  $2
   30     5      > RETURN                                                   null

End of function __construct

End of class Model.

Class Logger:
Function log:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  log
number of ops:  3
compiled vars:  !0 = $log
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        ECHO                                                     !0
   39     2      > RETURN                                                   null

End of function log

End of class Logger.

Class CommentCount:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  __construct
number of ops:  4
compiled vars:  !0 = $count
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
   49     1        ASSIGN_OBJ                                               'count'
          2        OP_DATA                                                  !0
   50     3      > RETURN                                                   null

End of function __construct

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

End of function getcount

End of class CommentCount.

Class CommentWow:
Function wow:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/jKuBi
function name:  wow
number of ops:  23
compiled vars:  !0 = $commentCount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        INIT_METHOD_CALL                                         !0, 'getCount'
          2        DO_FCALL                                      0  $1      
          3        IS_EQUAL                                                 $1, 0
          4      > JMPZ                                                     ~2, ->6
   64     5    > > RETURN                                                   'Sad.+%3A%28'
   67     6    >   INIT_METHOD_CALL                                         !0, 'getCount'
          7        DO_FCALL                                      0  $3      
          8        IS_SMALLER                                       ~4      0, $3
          9      > JMPZ_EX                                          ~4      ~4, ->14
         10    >   INIT_METHOD_CALL                                         !0, 'getCount'
         11        DO_FCALL                                      0  $5      
         12        IS_SMALLER                                       ~6      $5, 5
         13        BOOL                                             ~4      ~6
         14    > > JMPZ                                                     ~4, ->16
   68    15    > > RETURN                                                   'Ok.'
   71    16    >   INIT_METHOD_CALL                                         !0, 'getCount'
         17        DO_FCALL                                      0  $7      
         18        IS_SMALLER_OR_EQUAL                                      5, $7
         19      > JMPZ                                                     ~8, ->21
   72    20    > > RETURN                                                   'Wow%21+%3A%29'
   74    21    >   VERIFY_RETURN_TYPE                                       
         22      > RETURN                                                   null

End of function wow

End of class CommentWow.

Class Post:
Function commentcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 9
Branch analysis from position: 9
2 jumps found. (Code = 107) Position 1 = 10, Position 2 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  commentCount
number of ops:  22
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   NEW                                              $1      'CommentCount'
          1        FETCH_OBJ_R                                      ~2      'comments'
          2        INIT_METHOD_CALL                                         ~2, 'count'
          3        DO_FCALL                                      0  $3      
          4        SEND_VAR_NO_REF_EX                                       $3
          5        DO_FCALL                                      0          
          6        VERIFY_RETURN_TYPE                                       $1
          7      > RETURN                                                   $1
          8*       JMP                                                      ->20
   85     9  E > > CATCH                                       last         'Exception'
   86    10    >   INIT_STATIC_METHOD_CALL                                  'Logger', 'log'
         11        INIT_METHOD_CALL                                         !0, 'getMessage'
         12        DO_FCALL                                      0  $5      
         13        SEND_VAR                                                 $5
         14        DO_FCALL                                      0          
   87    15        NEW                                              $7      'CommentCount'
         16        SEND_VAL_EX                                              0
         17        DO_FCALL                                      0          
         18        VERIFY_RETURN_TYPE                                       $7
         19      > RETURN                                                   $7
   89    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function commentcount

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/jKuBi
function name:  __construct
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_ANON_CLASS                               <unknown> 
          1        NEW                                              $2      $1
          2        DO_FCALL                                      0          
          3        ASSIGN_OBJ                                               'comments'
          4        OP_DATA                                                  $2
   30     5      > RETURN                                                   null

End of function __construct

End of class Post.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.92 ms | 1473 KiB | 15 Q