3v4l.org

run code in 300+ PHP versions simultaneously
<?php // 実行 $testNestedTransactions = new TestNestedTransactions(); $testNestedTransactions->createTables(); // CREATE TABLE $result = $testNestedTransactions->insert(); // INSERT echo "<p>{$result}</p>"; // 結果 // データベース接続 final class Database { private static array $instances = []; // データベース接続 final public static function getInstance(string $name): \PDO { if (!isset(self::$instances[$name])) { // データベース接続情報 $config = [ 'db1' => [ 'dsn' => 'mysql:host=localhost;dbname=mydb1', 'username' => 'myuser1', 'password' => 'mypassword1' ] ]; if (!isset($config[$name])) { throw new \DomainException(json_encode_noesc_slashes([ 'message' => 'Invalid database name: ' . $name ])); } $dbConfig = $config[$name]; $dsn = $dbConfig['dsn']; $username = $dbConfig['username']; $password = $dbConfig['password']; self::$instances[$name] = new \PDO($dsn, $username, $password); } return self::$instances[$name]; } } // 入れ子のトランザクション final class TestNestedTransactions { private \PDO $pdo; public function __construct() { $this->pdo = Database::getInstance('db1'); } // テーブル作成 final public function createTables(): void { try { $this->pdo->query(" CREATE TABLE IF NOT EXISTS test_table1 ( id INT AUTO_INCREMENT PRIMARY KEY, column1 VARCHAR(255) NOT NULL ) "); $this->pdo->query(" CREATE TABLE IF NOT EXISTS test_table2 ( id INT AUTO_INCREMENT PRIMARY KEY, column2 VARCHAR(255) NOT NULL ) "); } catch (\PDOException $e) { echo $e->getMessage(); } } // 入れ子のトランザクションで INSERT を実行 final public function insert(): string { try { // 外側のトランザクション開始 $this->pdo->beginTransaction(); // test_table1 の INSERT を実行 $stmt1 = $this->pdo->prepare("INSERT INTO test_table1 (column1) VALUES (:value1)"); $value1 = 'some_value_1'; $stmt1->bindParam(':value1', $value1); $stmt1->execute(); try { // 入れ子のトランザクション開始 $this->pdo->beginTransaction(); // test_table2 の INSERT を実行 $stmt2 = $this->pdo->prepare("INSERT INTO test_table2 (column2) VALUES (:value2)"); $value2 = 'some_value_2'; $stmt2->bindParam(':value2', $value2); $stmt2->execute(); // 入れ子のトランザクションのコミット $this->pdo->commit(); } catch (\Exception $e) { // 入れ子のトランザクションのロールバック $this->pdo->rollBack(); throw $e; } // 外側のトランザクションのコミット $this->pdo->commit(); return "成功"; } catch (\Exception $e) { // 外側のトランザクションのロールバック $this->pdo->rollBack(); // この行でエラーが発生 return "エラー: " . $e->getMessage(); } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HivEd
function name:  (null)
number of ops:  13
compiled vars:  !0 = $testNestedTransactions, !1 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   NEW                                              $2      'TestNestedTransactions'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
    5     3        INIT_METHOD_CALL                                         !0, 'createTables'
          4        DO_FCALL                                      0          
    6     5        INIT_METHOD_CALL                                         !0, 'insert'
          6        DO_FCALL                                      0  $6      
          7        ASSIGN                                                   !1, $6
    7     8        ROPE_INIT                                     3  ~9      '%3Cp%3E'
          9        ROPE_ADD                                      1  ~9      ~9, !1
         10        ROPE_END                                      2  ~8      ~9, '%3C%2Fp%3E'
         11        ECHO                                                     ~8
  115    12      > RETURN                                                   1

Class Database:
Function getinstance:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 34
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/HivEd
function name:  getInstance
number of ops:  40
compiled vars:  !0 = $name, !1 = $config, !2 = $dbConfig, !3 = $dsn, !4 = $username, !5 = $password
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   17     1        FETCH_STATIC_PROP_IS                             ~6      'instances'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~7      ~6, !0
          3        BOOL_NOT                                         ~8      ~7
          4      > JMPZ                                                     ~8, ->34
   19     5    >   ASSIGN                                                   !1, <array>
   27     6        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     !1, !0
          7        BOOL_NOT                                         ~11     ~10
          8      > JMPZ                                                     ~11, ->18
   28     9    >   NEW                                              $12     'DomainException'
         10        INIT_FCALL_BY_NAME                                       'json_encode_noesc_slashes'
   29    11        CONCAT                                           ~13     'Invalid+database+name%3A+', !0
         12        INIT_ARRAY                                       ~14     ~13, 'message'
         13        SEND_VAL_EX                                              ~14
   28    14        DO_FCALL                                      0  $15     
   29    15        SEND_VAR_NO_REF_EX                                       $15
   28    16        DO_FCALL                                      0          
   29    17      > THROW                                         0          $12
   32    18    >   FETCH_DIM_R                                      ~17     !1, !0
         19        ASSIGN                                                   !2, ~17
   33    20        FETCH_DIM_R                                      ~19     !2, 'dsn'
         21        ASSIGN                                                   !3, ~19
   34    22        FETCH_DIM_R                                      ~21     !2, 'username'
         23        ASSIGN                                                   !4, ~21
   35    24        FETCH_DIM_R                                      ~23     !2, 'password'
         25        ASSIGN                                                   !5, ~23
   36    26        NEW                                              $27     'PDO'
         27        SEND_VAR_EX                                              !3
         28        SEND_VAR_EX                                              !4
         29        SEND_VAR_EX                                              !5
         30        DO_FCALL                                      0          
         31        FETCH_STATIC_PROP_W          unknown             $25     'instances'
         32        ASSIGN_DIM                                               $25, !0
         33        OP_DATA                                                  $27
   38    34    >   FETCH_STATIC_PROP_R          unknown             ~29     'instances'
         35        FETCH_DIM_R                                      ~30     ~29, !0
         36        VERIFY_RETURN_TYPE                                       ~30
         37      > RETURN                                                   ~30
   39    38*       VERIFY_RETURN_TYPE                                       
         39*     > RETURN                                                   null

End of function getinstance

End of class Database.

Class TestNestedTransactions:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HivEd
function name:  __construct
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   49     0  E >   INIT_STATIC_METHOD_CALL                                  'Database', 'getInstance'
          1        SEND_VAL                                                 'db1'
          2        DO_FCALL                                      0  $1      
          3        ASSIGN_OBJ                                               'pdo'
          4        OP_DATA                                                  $1
   50     5      > RETURN                                                   null

End of function __construct

Function createtables:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
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/HivEd
function name:  createTables
number of ops:  14
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   56     0  E >   FETCH_OBJ_R                                      ~1      'pdo'
          1        INIT_METHOD_CALL                                         ~1, 'query'
          2        SEND_VAL_EX                                              '%0A++++++++++++++++CREATE+TABLE+IF+NOT+EXISTS+test_table1+%28%0A++++++++++++++++++++id+INT+AUTO_INCREMENT+PRIMARY+KEY%2C%0A++++++++++++++++++++column1+VARCHAR%28255%29+NOT+NULL%0A++++++++++++++++%29%0A++++++++++++'
          3        DO_FCALL                                      0          
   62     4        FETCH_OBJ_R                                      ~3      'pdo'
          5        INIT_METHOD_CALL                                         ~3, 'query'
          6        SEND_VAL_EX                                              '%0A++++++++++++++++CREATE+TABLE+IF+NOT+EXISTS+test_table2+%28%0A++++++++++++++++++++id+INT+AUTO_INCREMENT+PRIMARY+KEY%2C%0A++++++++++++++++++++column2+VARCHAR%28255%29+NOT+NULL%0A++++++++++++++++%29%0A++++++++++++'
          7        DO_FCALL                                      0          
          8      > JMP                                                      ->13
   69     9  E > > CATCH                                       last         'PDOException'
   70    10    >   INIT_METHOD_CALL                                         !0, 'getMessage'
         11        DO_FCALL                                      0  $5      
         12        ECHO                                                     $5
   72    13    > > RETURN                                                   null

End of function createtables

Function insert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 34
Branch analysis from position: 34
2 jumps found. (Code = 107) Position 1 = 35, Position 2 = -2
Branch analysis from position: 35
1 jumps found. (Code = 108) Position 1 = -2
Found catch point at position: 44
Branch analysis from position: 44
2 jumps found. (Code = 107) Position 1 = 45, Position 2 = -2
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/HivEd
function name:  insert
number of ops:  55
compiled vars:  !0 = $stmt1, !1 = $value1, !2 = $stmt2, !3 = $value2, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   FETCH_OBJ_R                                      ~5      'pdo'
          1        INIT_METHOD_CALL                                         ~5, 'beginTransaction'
          2        DO_FCALL                                      0          
   82     3        FETCH_OBJ_R                                      ~7      'pdo'
          4        INIT_METHOD_CALL                                         ~7, 'prepare'
          5        SEND_VAL_EX                                              'INSERT+INTO+test_table1+%28column1%29+VALUES+%28%3Avalue1%29'
          6        DO_FCALL                                      0  $8      
          7        ASSIGN                                                   !0, $8
   83     8        ASSIGN                                                   !1, 'some_value_1'
   84     9        INIT_METHOD_CALL                                         !0, 'bindParam'
         10        SEND_VAL_EX                                              '%3Avalue1'
         11        SEND_VAR_EX                                              !1
         12        DO_FCALL                                      0          
   85    13        INIT_METHOD_CALL                                         !0, 'execute'
         14        DO_FCALL                                      0          
   89    15        FETCH_OBJ_R                                      ~13     'pdo'
         16        INIT_METHOD_CALL                                         ~13, 'beginTransaction'
         17        DO_FCALL                                      0          
   92    18        FETCH_OBJ_R                                      ~15     'pdo'
         19        INIT_METHOD_CALL                                         ~15, 'prepare'
         20        SEND_VAL_EX                                              'INSERT+INTO+test_table2+%28column2%29+VALUES+%28%3Avalue2%29'
         21        DO_FCALL                                      0  $16     
         22        ASSIGN                                                   !2, $16
   93    23        ASSIGN                                                   !3, 'some_value_2'
   94    24        INIT_METHOD_CALL                                         !2, 'bindParam'
         25        SEND_VAL_EX                                              '%3Avalue2'
         26        SEND_VAR_EX                                              !3
         27        DO_FCALL                                      0          
   95    28        INIT_METHOD_CALL                                         !2, 'execute'
         29        DO_FCALL                                      0          
   98    30        FETCH_OBJ_R                                      ~21     'pdo'
         31        INIT_METHOD_CALL                                         ~21, 'commit'
         32        DO_FCALL                                      0          
         33      > JMP                                                      ->39
   99    34  E > > CATCH                                       last         'Exception'
  101    35    >   FETCH_OBJ_R                                      ~23     'pdo'
         36        INIT_METHOD_CALL                                         ~23, 'rollBack'
         37        DO_FCALL                                      0          
  102    38      > THROW                                         0          !4
  106    39    >   FETCH_OBJ_R                                      ~25     'pdo'
         40        INIT_METHOD_CALL                                         ~25, 'commit'
         41        DO_FCALL                                      0          
  108    42      > RETURN                                                   '%E6%88%90%E5%8A%9F'
         43*       JMP                                                      ->53
  109    44  E > > CATCH                                       last         'Exception'
  111    45    >   FETCH_OBJ_R                                      ~27     'pdo'
         46        INIT_METHOD_CALL                                         ~27, 'rollBack'
         47        DO_FCALL                                      0          
  112    48        INIT_METHOD_CALL                                         !4, 'getMessage'
         49        DO_FCALL                                      0  $29     
         50        CONCAT                                           ~30     '%E3%82%A8%E3%83%A9%E3%83%BC%3A+', $29
         51        VERIFY_RETURN_TYPE                                       ~30
         52      > RETURN                                                   ~30
  114    53*       VERIFY_RETURN_TYPE                                       
         54*     > RETURN                                                   null

End of function insert

End of class TestNestedTransactions.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
148.08 ms | 1020 KiB | 13 Q