3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Database { protected static $instance; protected $pdo; protected function __construct() { static $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]; $this->pdo = new PDO('sqlite::memory:', null, null, $opt); } public static function instance() { if (null === self::$instance) { self::$instance = new self; } return self::$instance; } public function run($sql, $args = []) { if (!$args) { try { return $this->query($sql); } catch(\PDOException $e) { throw new \PDOQueryException($e->getMessage(), (int) $e->getCode(), $e); } } try { $stmt = $this->prepare($sql); } catch(\PDOException $e) { throw new \PDOPrepareException($e->getMessage(), (int) $e->getCode(), $e); } try { $stmt->execute($args); } catch(\PDOException $e) { throw new \PDOExecuteException($e->getMessage(), (int) $e->getCode(), $e); } return $stmt; } public function __call($method, $args) { if (is_callable([$this->pdo, $method])) { //php 5.6+ variadic optimization (aka splat operator) return $this->pdo->$method(...$args); //PHP <= 5.5 //return call_user_func_array(array($this->pdo, $method), $args); } throw new \BadMethodCallException(sprintf('Unknown method PDO::%s called!', $method)); } } interface DatabaseRunException {} class PDOPrepareException extends PDOException implements DatabaseRunException{} class PDOExecuteException extends PDOException implements DatabaseRunException{} class PDOQueryException extends PDOException implements DatabaseRunException{} //RunTime code if (PHP_VERSION_ID >= 70100) { try { $db = Database::instance(); var_dump($db->run('SELECT ?', ['foo', 'bar'])->fetch()); } catch(\PDOQueryException $e) { //Handle the query exception } catch(\PDOPrepareException $e) { //Handle the prepare exception } catch(\PDOExecuteException $e) { echo 'PDO::execute() failed'; //Handle the execute exception } } else { try { $db = Database::instance(); var_dump($db->run('SELECT ?', ['foo', 'bar'])->fetch()); } catch(\DatabaseRunException $e) { if ($e instanceof \PDOQueryException) { //Handle the query exception } elseif ($e instanceof \PDOPrepareException) { //Handle the prepare exception } elseif($e instanceof \PDOExecuteException) { echo 'PDO::execute() failed'; //Handle the execute exception } } }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Found catch point at position: 17
Branch analysis from position: 17
2 jumps found. (Code = 107) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 19
2 jumps found. (Code = 107) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 21
2 jumps found. (Code = 107) Position 1 = 22, Position 2 = -2
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Found catch point at position: 19
Branch analysis from position: 19
Found catch point at position: 21
Branch analysis from position: 21
Found catch point at position: 37
Branch analysis from position: 37
2 jumps found. (Code = 107) Position 1 = 38, Position 2 = -2
Branch analysis from position: 38
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 41
Branch analysis from position: 40
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 44
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 47
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
filename:       /in/8QoRF
function name:  (null)
number of ops:  48
compiled vars:  !0 = $db, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   DECLARE_CLASS                                            'pdoprepareexception', 'pdoexception'
   62     1        DECLARE_CLASS                                            'pdoexecuteexception', 'pdoexception'
   63     2        DECLARE_CLASS                                            'pdoqueryexception', 'pdoexception'
   68     3      > JMPZ                                                     <true>, ->24
   70     4    >   INIT_STATIC_METHOD_CALL                                  'Database', 'instance'
          5        DO_FCALL                                      0  $2      
          6        ASSIGN                                                   !0, $2
   71     7        INIT_FCALL                                               'var_dump'
          8        INIT_METHOD_CALL                                         !0, 'run'
          9        SEND_VAL_EX                                              'SELECT+%3F'
         10        SEND_VAL_EX                                              <array>
         11        DO_FCALL                                      0  $4      
         12        INIT_METHOD_CALL                                         $4, 'fetch'
         13        DO_FCALL                                      0  $5      
         14        SEND_VAR                                                 $5
         15        DO_ICALL                                                 
         16      > JMP                                                      ->23
   72    17  E > > CATCH                                                    'PDOQueryException', ->19
         18    > > JMP                                                      ->23
   74    19  E > > CATCH                                                    'PDOPrepareException', ->21
         20    > > JMP                                                      ->23
   76    21  E > > CATCH                                       last         'PDOExecuteException'
   77    22    >   ECHO                                                     'PDO%3A%3Aexecute%28%29+failed'
   68    23    > > JMP                                                      ->47
   82    24    >   INIT_STATIC_METHOD_CALL                                  'Database', 'instance'
         25        DO_FCALL                                      0  $7      
         26        ASSIGN                                                   !0, $7
   83    27        INIT_FCALL                                               'var_dump'
         28        INIT_METHOD_CALL                                         !0, 'run'
         29        SEND_VAL_EX                                              'SELECT+%3F'
         30        SEND_VAL_EX                                              <array>
         31        DO_FCALL                                      0  $9      
         32        INIT_METHOD_CALL                                         $9, 'fetch'
         33        DO_FCALL                                      0  $10     
         34        SEND_VAR                                                 $10
         35        DO_ICALL                                                 
         36      > JMP                                                      ->47
   84    37  E > > CATCH                                       last         'DatabaseRunException'
   85    38    >   INSTANCEOF                                               !1, 'PDOQueryException'
         39      > JMPZ                                                     ~12, ->41
         40    > > JMP                                                      ->47
   87    41    >   INSTANCEOF                                               !1, 'PDOPrepareException'
         42      > JMPZ                                                     ~13, ->44
         43    > > JMP                                                      ->47
   89    44    >   INSTANCEOF                                               !1, 'PDOExecuteException'
         45      > JMPZ                                                     ~14, ->47
   90    46    >   ECHO                                                     'PDO%3A%3Aexecute%28%29+failed'
   94    47    > > RETURN                                                   1

Class Database:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/8QoRF
function name:  __construct
number of ops:  10
compiled vars:  !0 = $opt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   BIND_STATIC                                              !0
   13     1        NEW                                              $2      'PDO'
          2        SEND_VAL_EX                                              'sqlite%3A%3Amemory%3A'
          3        SEND_VAL_EX                                              null
          4        SEND_VAL_EX                                              null
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0          
          7        ASSIGN_OBJ                                               'pdo'
          8        OP_DATA                                                  $2
   14     9      > RETURN                                                   null

End of function __construct

Function instance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/8QoRF
function name:  instance
number of ops:  10
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   FETCH_STATIC_PROP_R          unknown             ~0      'instance'
          1        TYPE_CHECK                                    2          ~0
          2      > JMPZ                                                     ~1, ->7
   19     3    >   NEW                          self                $3      
          4        DO_FCALL                                      0          
          5        ASSIGN_STATIC_PROP                                       'instance'
          6        OP_DATA                                                  $3
   22     7    >   FETCH_STATIC_PROP_R          unknown             ~5      'instance'
          8      > RETURN                                                   ~5
   23     9*     > RETURN                                                   null

End of function instance

Function run:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
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 = 108) Position 1 = -2
Found catch point at position: 26
Branch analysis from position: 26
2 jumps found. (Code = 107) Position 1 = 27, Position 2 = -2
Branch analysis from position: 27
1 jumps found. (Code = 108) Position 1 = -2
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 = 108) Position 1 = -2
filename:       /in/8QoRF
function name:  run
number of ops:  56
compiled vars:  !0 = $sql, !1 = $args, !2 = $e, !3 = $stmt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   27     2        BOOL_NOT                                         ~4      !1
          3      > JMPZ                                                     ~4, ->21
   29     4    >   INIT_METHOD_CALL                                         'query'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $5      
          7      > RETURN                                                   $5
          8*       JMP                                                      ->21
   30     9  E > > CATCH                                       last         'PDOException'
   31    10    >   NEW                                              $6      'PDOQueryException'
         11        INIT_METHOD_CALL                                         !2, 'getMessage'
         12        DO_FCALL                                      0  $7      
         13        SEND_VAR_NO_REF_EX                                       $7
         14        INIT_METHOD_CALL                                         !2, 'getCode'
         15        DO_FCALL                                      0  $8      
         16        CAST                                          4  ~9      $8
         17        SEND_VAL_EX                                              ~9
         18        SEND_VAR_EX                                              !2
         19        DO_FCALL                                      0          
         20      > THROW                                         0          $6
   35    21    >   INIT_METHOD_CALL                                         'prepare'
         22        SEND_VAR_EX                                              !0
         23        DO_FCALL                                      0  $11     
         24        ASSIGN                                                   !3, $11
         25      > JMP                                                      ->38
   36    26  E > > CATCH                                       last         'PDOException'
   37    27    >   NEW                                              $13     'PDOPrepareException'
         28        INIT_METHOD_CALL                                         !2, 'getMessage'
         29        DO_FCALL                                      0  $14     
         30        SEND_VAR_NO_REF_EX                                       $14
         31        INIT_METHOD_CALL                                         !2, 'getCode'
         32        DO_FCALL                                      0  $15     
         33        CAST                                          4  ~16     $15
         34        SEND_VAL_EX                                              ~16
         35        SEND_VAR_EX                                              !2
         36        DO_FCALL                                      0          
         37      > THROW                                         0          $13
   40    38    >   INIT_METHOD_CALL                                         !3, 'execute'
         39        SEND_VAR_EX                                              !1
         40        DO_FCALL                                      0          
         41      > JMP                                                      ->54
   41    42  E > > CATCH                                       last         'PDOException'
   42    43    >   NEW                                              $19     'PDOExecuteException'
         44        INIT_METHOD_CALL                                         !2, 'getMessage'
         45        DO_FCALL                                      0  $20     
         46        SEND_VAR_NO_REF_EX                                       $20
         47        INIT_METHOD_CALL                                         !2, 'getCode'
         48        DO_FCALL                                      0  $21     
         49        CAST                                          4  ~22     $21
         50        SEND_VAL_EX                                              ~22
         51        SEND_VAR_EX                                              !2
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $19
   45    54    > > RETURN                                                   !3
   46    55*     > RETURN                                                   null

End of function run

Function __call:
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 = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/8QoRF
function name:  __call
number of ops:  24
compiled vars:  !0 = $method, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   50     2        INIT_FCALL                                               'is_callable'
          3        FETCH_OBJ_R                                      ~2      'pdo'
          4        INIT_ARRAY                                       ~3      ~2
          5        ADD_ARRAY_ELEMENT                                ~3      !0
          6        SEND_VAL                                                 ~3
          7        DO_ICALL                                         $4      
          8      > JMPZ                                                     $4, ->15
   52     9    >   FETCH_OBJ_R                                      ~5      'pdo'
         10        INIT_METHOD_CALL                                         ~5, !0
         11        SEND_UNPACK                                              !1
         12        CHECK_UNDEF_ARGS                                         
         13        DO_FCALL                                      1  $6      
         14      > RETURN                                                   $6
   56    15    >   NEW                                              $7      'BadMethodCallException'
         16        INIT_FCALL                                               'sprintf'
         17        SEND_VAL                                                 'Unknown+method+PDO%3A%3A%25s+called%21'
         18        SEND_VAR                                                 !0
         19        DO_ICALL                                         $8      
         20        SEND_VAR_NO_REF_EX                                       $8
         21        DO_FCALL                                      0          
         22      > THROW                                         0          $7
   57    23*     > RETURN                                                   null

End of function __call

End of class Database.

Class DatabaseRunException: [no user functions]
Class PDOPrepareException: [no user functions]
Class PDOExecuteException: [no user functions]
Class PDOQueryException: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
234.79 ms | 1021 KiB | 16 Q