3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Define a custom exception class */ class MyException extends Exception { // Redefine the exception so message isn't optional public function __construct($message, $code = 0, Exception $previous = null) { // some code // make sure everything is assigned properly parent::__construct($message, $code, $previous); } // custom string representation of object public function __toString() { return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; } public function customFunction() { echo "A custom function for this type of exception\n"; } } /** * Create a class to test the exception */ class TestException { public $var; const THROW_NONE = 0; const THROW_CUSTOM = 1; const THROW_DEFAULT = 2; function __construct($avalue = self::THROW_NONE) { switch ($avalue) { case self::THROW_CUSTOM: // throw custom exception throw new MyException('1 is an invalid parameter', 5); break; case self::THROW_DEFAULT: // throw default one. throw new Exception('2 is not allowed as a parameter', 6); break; default: // No exception, object will be created. $this->var = $avalue; break; } } } // Example 1 try { $o = new TestException(TestException::THROW_CUSTOM); } catch (MyException $e) { // Will be caught echo "Caught my exception\n", $e; $e->customFunction(); } catch (Exception $e) { // Skipped echo "Caught Default Exception\n", $e; } // Continue execution var_dump($o); // Null echo "\n\n"; // Example 2 try { $o = new TestException(TestException::THROW_DEFAULT); } catch (MyException $e) { // Doesn't match this type echo "Caught my exception\n", $e; $e->customFunction(); } catch (Exception $e) { // Will be caught echo "Caught Default Exception\n", $e; } // Continue execution var_dump($o); // Null echo "\n\n"; // Example 3 try { $o = new TestException(TestException::THROW_CUSTOM); } catch (Exception $e) { // Will be caught echo "Default Exception caught\n", $e; } // Continue execution var_dump($o); // Null echo "\n\n"; // Example 4 try { $o = new TestException(); } catch (Exception $e) { // Skipped, no exception echo "Default Exception caught\n", $e; } // Continue execution var_dump($o); // TestException echo "\n\n"; ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 6
Branch analysis from position: 6
2 jumps found. (Code = 107) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
Branch analysis from position: 12
2 jumps found. (Code = 107) Position 1 = 13, Position 2 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Found catch point at position: 12
Branch analysis from position: 12
Found catch point at position: 24
Branch analysis from position: 24
2 jumps found. (Code = 107) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 33
Branch analysis from position: 33
Branch analysis from position: 30
2 jumps found. (Code = 107) Position 1 = 31, Position 2 = -2
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Found catch point at position: 30
Branch analysis from position: 30
Found catch point at position: 42
Branch analysis from position: 42
2 jumps found. (Code = 107) Position 1 = 43, Position 2 = -2
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
Found catch point at position: 53
Branch analysis from position: 53
2 jumps found. (Code = 107) Position 1 = 54, Position 2 = -2
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FkHr5
function name:  (null)
number of ops:  61
compiled vars:  !0 = $o, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   DECLARE_CLASS                                            'myexception', 'exception'
   61     1        NEW                                              $2      'TestException'
          2        SEND_VAL_EX                                              1
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $2
          5      > JMP                                                      ->15
   62     6  E > > CATCH                                                    'MyException', ->12
   63     7    >   ECHO                                                     'Caught+my+exception%0A'
          8        ECHO                                                     !1
   64     9        INIT_METHOD_CALL                                         !1, 'customFunction'
         10        DO_FCALL                                      0          
         11      > JMP                                                      ->15
   65    12  E > > CATCH                                       last         'Exception'
   66    13    >   ECHO                                                     'Caught+Default+Exception%0A'
         14        ECHO                                                     !1
   70    15    >   INIT_FCALL                                               'var_dump'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                                 
   71    18        ECHO                                                     '%0A%0A'
   76    19        NEW                                              $7      'TestException'
         20        SEND_VAL_EX                                              2
         21        DO_FCALL                                      0          
         22        ASSIGN                                                   !0, $7
         23      > JMP                                                      ->33
   77    24  E > > CATCH                                                    'MyException', ->30
   78    25    >   ECHO                                                     'Caught+my+exception%0A'
         26        ECHO                                                     !1
   79    27        INIT_METHOD_CALL                                         !1, 'customFunction'
         28        DO_FCALL                                      0          
         29      > JMP                                                      ->33
   80    30  E > > CATCH                                       last         'Exception'
   81    31    >   ECHO                                                     'Caught+Default+Exception%0A'
         32        ECHO                                                     !1
   85    33    >   INIT_FCALL                                               'var_dump'
         34        SEND_VAR                                                 !0
         35        DO_ICALL                                                 
   86    36        ECHO                                                     '%0A%0A'
   91    37        NEW                                              $12     'TestException'
         38        SEND_VAL_EX                                              1
         39        DO_FCALL                                      0          
         40        ASSIGN                                                   !0, $12
         41      > JMP                                                      ->45
   92    42  E > > CATCH                                       last         'Exception'
   93    43    >   ECHO                                                     'Default+Exception+caught%0A'
         44        ECHO                                                     !1
   97    45    >   INIT_FCALL                                               'var_dump'
         46        SEND_VAR                                                 !0
         47        DO_ICALL                                                 
   98    48        ECHO                                                     '%0A%0A'
  103    49        NEW                                              $16     'TestException'
         50        DO_FCALL                                      0          
         51        ASSIGN                                                   !0, $16
         52      > JMP                                                      ->56
  104    53  E > > CATCH                                       last         'Exception'
  105    54    >   ECHO                                                     'Default+Exception+caught%0A'
         55        ECHO                                                     !1
  109    56    >   INIT_FCALL                                               'var_dump'
         57        SEND_VAR                                                 !0
         58        DO_ICALL                                                 
  110    59        ECHO                                                     '%0A%0A'
  111    60      > RETURN                                                   1

Class MyException:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FkHr5
function name:  __construct
number of ops:  9
compiled vars:  !0 = $message, !1 = $code, !2 = $previous
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      null
   12     3        INIT_STATIC_METHOD_CALL                                  
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0          
   13     8      > RETURN                                                   null

End of function __construct

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FkHr5
function name:  __toString
number of ops:  12
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   ROPE_INIT                                     5  ~3      '%3A+%5B'
          1        FETCH_OBJ_R                                      ~0      'code'
          2        ROPE_ADD                                      1  ~3      ~3, ~0
          3        ROPE_ADD                                      2  ~3      ~3, '%5D%3A+'
          4        FETCH_OBJ_R                                      ~1      'message'
          5        ROPE_ADD                                      3  ~3      ~3, ~1
          6        ROPE_END                                      4  ~2      ~3, '%0A'
          7        CONCAT                                           ~6      'MyException', ~2
          8        VERIFY_RETURN_TYPE                                       ~6
          9      > RETURN                                                   ~6
   18    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function __tostring

Function customfunction:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FkHr5
function name:  customFunction
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ECHO                                                     'A+custom+function+for+this+type+of+exception%0A'
   22     1      > RETURN                                                   null

End of function customfunction

End of class MyException.

Class TestException:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 12
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/FkHr5
function name:  __construct
number of ops:  22
compiled vars:  !0 = $avalue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV_INIT                                        !0      <const ast>
   39     1        IS_EQUAL                                                 !0, 1
          2      > JMPNZ                                                    ~1, ->6
          3    >   IS_EQUAL                                                 !0, 2
          4      > JMPNZ                                                    ~1, ->12
          5    > > JMP                                                      ->18
   42     6    >   NEW                                              $2      'MyException'
          7        SEND_VAL_EX                                              '1+is+an+invalid+parameter'
          8        SEND_VAL_EX                                              5
          9        DO_FCALL                                      0          
         10      > THROW                                         0          $2
   43    11*       JMP                                                      ->21
   47    12    >   NEW                                              $4      'Exception'
         13        SEND_VAL_EX                                              '2+is+not+allowed+as+a+parameter'
         14        SEND_VAL_EX                                              6
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $4
   48    17*       JMP                                                      ->21
   52    18    >   ASSIGN_OBJ                                               'var'
         19        OP_DATA                                                  !0
   53    20      > JMP                                                      ->21
   55    21    > > RETURN                                                   null

End of function __construct

End of class TestException.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.55 ms | 1408 KiB | 15 Q