Connection
in package
Singleton class to manage database connection.
This class uses the Singleton pattern to ensure that only one instance of the database connection is created and reused during the application's lifecycle. The connection is established using PDO with a database path defined by an environment variable.
Table of Contents
Properties
- $connection : PDO
- PDO instance for the database connection.
- $instance : self|null
- Single instance of the connection.
- $path : string
- Database path.
Methods
- __construct() : mixed
- Class constructor.
- getInstance() : PDO
- Gets the single instance of the connection.
- resetInstance() : void
- Reset the instance for testing purposes.
- setInstance() : void
- Sets the instance as an in-memory connection for testing purposes.
Properties
$connection
PDO instance for the database connection.
private
PDO
$connection
$instance
Single instance of the connection.
private
static self|null
$instance
= null
$path
Database path.
private
string
$path
= '/workspaces/live_ecommerce/App/Infrastructure/Persistence/library.db'
Methods
__construct()
Class constructor.
public
__construct() : mixed
The constructor is private to prevent creating multiple instances of the class. It initializes the database connection using the path specified in the DB_PATH environment variable.
Tags
getInstance()
Gets the single instance of the connection.
public
static getInstance() : PDO
Return values
PDO —The PDO instance for the database connection.
resetInstance()
Reset the instance for testing purposes.
public
static resetInstance() : void
setInstance()
Sets the instance as an in-memory connection for testing purposes.
public
static setInstance(PDO $connection) : void
Parameters
- $connection : PDO
-
The PDO instance to use.