Content · Web Development · PHP

Building Reusable Database Connection Factories

A connection factory centralizes DSN construction and safe PDO options. Credentials should come from environment or a secret store, not source control.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

A connection factory centralizes DSN construction and safe PDO options.

The examples focus on the operation itself, including the important distinction: A factory returns PDO instances; repositories perform queries with them.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Before You Start


The examples use PHP 8.2+ features where shown. Database snippets require the corresponding PDO driver, demo tables, and credentials configured outside source code.

Example 1: Basic Use


The factory centralizes PDO initialization and takes credentials from server-side configuration.

<?php
final class PdoFactory {
    public static function mysql(string $dsn, string $user, string $password): PDO {
        return new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    }
}
$dsn = "mysql:host=127.0.0.1;dbname=demo;charset=utf8mb4";
$pdo = PdoFactory::mysql($dsn, getenv("DB_USER") ?: "app", getenv("DB_PASSWORD") ?: "");
echo $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


The factory centralizes PDO initialization and takes credentials from server-side configuration. A caller can use the same factory for configuration checks and queries.

<?php
final class PdoFactory {
    public static function mysql(string $dsn, string $user, string $password): PDO {
        return new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    }
}
$dsn = "mysql:host=127.0.0.1;dbname=demo;charset=utf8mb4";
$pdo = PdoFactory::mysql($dsn, getenv("DB_USER") ?: "app", getenv("DB_PASSWORD") ?: "");
echo $pdo->getAttribute(PDO::ATTR_ERRMODE);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


The factory centralizes PDO initialization and takes credentials from server-side configuration. A caller can use the same factory for configuration checks and queries.

<?php
final class PdoFactory {
    public static function mysql(string $dsn, string $user, string $password): PDO {
        return new PDO($dsn, $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
    }
}
$dsn = "mysql:host=127.0.0.1;dbname=demo;charset=utf8mb4";
$pdo = PdoFactory::mysql($dsn, getenv("DB_USER") ?: "app", getenv("DB_PASSWORD") ?: "");
echo $pdo->query("SELECT 1")->fetchColumn();

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Credentials should come from environment or a secret store, not source control.

Comparison with Related Approaches


ApproachDifference
Building Reusable Database Connection FactoriesA connection factory centralizes DSN construction and safe PDO options.
Related API or alternativeA factory returns PDO instances; repositories perform queries with them.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsCredentials should come from environment or a secret store, not source control.
Confusing it with a related APIA factory returns PDO instances; repositories perform queries with them.

Key Takeaways


  • A connection factory centralizes DSN construction and safe PDO options.
  • Credentials should come from environment or a secret store, not source control.
  • A factory returns PDO instances; repositories perform queries with them.

Use Building Reusable Database Connection Factories when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.