class DB{ private $db_host = 'localhost'; private $db_user = 'root'; private $db_password = ''; private $db_name = 'mydatabase'; private $conn; function __construct(){ $this->conn = new mysqli($this->db_host, $this->db_user, $this->db_password, $this->db_name); if ($this->conn->connect_error) { die("Connection failed: " . $this->conn->connect_error); } } public function get_connection(){ return $this->conn; } }
class DB{ private $db_name = 'mydatabase.db'; private $conn; function __construct(){ $this->conn = new SQLite3($this->db_name); if (!$this->conn) { die("Connection failed: " . $this->conn->lastErrorMsg()); } } public function get_connection(){ return $this->conn; } }This example establishes a connection with a SQLite database using the SQLite3 package library. The __construct method initializes a new database connection object and throws an error if the connection attempt fails.