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
| Approach | Difference |
|---|---|
| Building Reusable Database Connection Factories | A connection factory centralizes DSN construction and safe PDO options. |
| Related API or alternative | A factory returns PDO instances; repositories perform queries with them. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Credentials should come from environment or a secret store, not source control. |
| Confusing it with a related API | A 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.