<?php
// .env 파일 로드
function loadEnvForClass() {
  $envFile = $_SERVER['DOCUMENT_ROOT'] . '/.env';
  if (file_exists($envFile)) {
    $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
      if (strpos($line, '=') !== false && strpos($line, '#') !== 0) {
        list($key, $value) = explode('=', $line, 2);
        $_ENV[trim($key)] = trim($value);
      }
    }
  }
}

class Dbh {
  private $host;
  private $user;
  private $pw;
  private $dbname;

  public function __construct() {
    loadEnvForClass();
    $this->host = $_ENV['DB_HOST'] ?? 'localhost';
    $this->dbname = $_ENV['DB_NAME'] ?? 'chandj';
    $this->user = $_ENV['DB_USER'] ?? 'chandj';
    $this->pw = $_ENV['DB_PASS'] ?? '';
  }

  function connect(){
    $dsn = "mysql:host=".$this->host.";dbname=".$this->dbname.";charset=utf8";
    $pdo = new PDO($dsn, $this->user, $this->pw);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
  }
}
?>