PHP code example of cwola / transaction

1. Go to this page and download the library: Download cwola/transaction library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cwola / transaction example snippets




use Cwola\Transaction;

$transaction = new Transaction\Transaction;
$transaction->begin();

echo $transaction->inTransaction()
    ? 'OPENED' : 'CLOSED';
// output : OPENED


$transaction->commit();

echo $transaction->inTransaction()
    ? 'OPENED' : 'CLOSED';
// output : CLOSED




use Cwola\Transaction;
use Cwola\Event;

$transaction = new Transaction\Transaction;

$transaction->addEventListener('beginning', function() {
    echo 'beginning... : ';
});
$transaction->addEventListener('begin', function(Event\Event $event) {
    echo sprintf('SUCCESS %s', $event->timeStamp) . PHP_EOL;
});

$transaction->addEventListener('rollbacking', function() {
    echo 'rollbacking... : ';
});
$transaction->addEventListener('rollback', function(Event\Event $event) {
    echo sprintf('SUCCESS %s', $event->timeStamp) . PHP_EOL;
});

$transaction->addEventListener('committing', function() {
    echo 'committing... : ';
});
$transaction->addEventListener('commit', function(Event\Event $event) {
    echo sprintf('SUCCESS %s', $event->timeStamp) . PHP_EOL;
});

$transaction->begin();
// output
// beginning... : SUCCESS 2022-08-22T23:32:51+09:00

echo $transaction->inTransaction()
    ? 'OPENED' : 'CLOSED';
// output : OPENED


$transaction->commit();
// output
// committing... : SUCCESS 2022-08-22T23:32:51+09:00

echo $transaction->inTransaction()
    ? 'OPENED' : 'CLOSED';
// output : CLOSED