PHP code example of rammewerk / database

1. Go to this page and download the library: Download rammewerk/database 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/ */

    

rammewerk / database example snippets


$database = new Rammewerk\Component\Database\Database('host','database','user','password','charset');

use Rammewerk\Component\Database\Database;
use Rammewerk\Component\Environment;

class DatabaseConnector {

    /** @var Database[] */
    protected array $instances = [];

    public function __construct(protected readonly Environment $environment) {}

    public function instance(string $database): Database {
        if( !isset( $this->instances[$database] ) ) {
            try {
                $this->instances[$database] = new Database(
                    $this->environment->get( 'DB_HOST' ),
                    $database,
                    $this->environment->get( 'DB_USERNAME' ),
                    $this->environment->get( 'DB_PASSWORD' ),
                    $this->environment->get( 'DB_CHARSET' )
                );
            } catch( \PDOException $e ) {
                // Log exception here...
                throw new \RuntimeException( 'Unable to connect database: ' . $database );
            }
        }

        return $this->instances[$database];

    }

}