<?php
class DB extends PDO
{
  public function __construct($dsn, $username = null, $password = null, array $options = null)
  {
    parent::__construct($dsn, $username, $password, $options);
  }

  static function openConnection($config = "/home/k/khofmann/public_html/phpCourse/configs/db.ini")
  {
    $conn = null;
    try {
      $dataAccess = parse_ini_file($config, true)["ZEDAT"];

      $conn = new PDO(
        "mysql:host={$dataAccess["host"]};dbname={$dataAccess["database"]};charset={$dataAccess["charset"]}",
        $dataAccess["user"],
        $dataAccess["passwd"],
        [
          PDO::ATTR_PERSISTENT => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
          PDO::ATTR_EMULATE_PREPARES   => false,
        ]
      );
    } catch (PDOException $exc) {
      echo $exc->getMessage();
    }
    return $conn;
  }

  static function closeConnection(&$conn)
  {
    $conn = null;
  }
}
