Database
in package
Class Database
A Singleton wrapper class for the PDO database connection. This class ensures that only one database connection instance exists throughout the lifecycle of a script execution, optimizing resource usage.
Table of Contents
Properties
Methods
- getConnection() : PDO
- Retrieves the active PDO connection object.
- getInstance() : Database
- Retrieves the single instance of the Database class.
- __construct() : mixed
- Database constructor.
Properties
$instance
private
static Database|null
$instance
= null
The single instance of the Database class.
$pdo
private
PDO
$pdo
The PHP Data Objects (PDO) connection instance.
Methods
getConnection()
Retrieves the active PDO connection object.
public
getConnection() : PDO
This object can be used to prepare statements and execute queries.
Return values
PDO —The PDO connection instance.
getInstance()
Retrieves the single instance of the Database class.
public
static getInstance() : Database
If the instance does not exist, it creates it. Otherwise, it returns the existing one.
Return values
Database —The Singleton database instance.
__construct()
Database constructor.
private
__construct() : mixed
Initializes the PDO connection with specific options:
- ERRMODE_EXCEPTION: Throws exceptions on errors.
- DEFAULT_FETCH_MODE: Returns arrays indexed by column name.
- EMULATE_PREPARES: Disables emulation for better security and native type handling.
The constructor is private to prevent direct instantiation (Singleton pattern).