If I want to create a connection class, is it OK to have the __construct as private? Or that will prevent users from connecting if there is more than one user at a time? Or I'm doing something that's completely wrong and this connection class should not even exist? (using connection without specific class would be better?)
code:
<?php
class Connection
{
private $host = "hostname";
private $db_name = "somedatabase";
private $username = "username";
private $password = "";
private $conn;
private function __construct()
{
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected Successfully Bro";
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
?>