PHPackages                             alaureati-enerds/pdofactory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. alaureati-enerds/pdofactory

ActiveLibrary[Database &amp; ORM](/categories/database)

alaureati-enerds/pdofactory
===========================

PHP Factory for PDO MySQL Connections

v1.1.7(9mo ago)023MITPHP

Since Nov 5Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/alaureati-enerds/pdofactory)[ Packagist](https://packagist.org/packages/alaureati-enerds/pdofactory)[ RSS](/packages/alaureati-enerds-pdofactory/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (10)Used By (0)

PDOFactory
==========

[](#pdofactory)

A lightweight and reusable PHP factory for creating a configured PDO instance using a typed configuration object.

Description
-----------

[](#description)

`PDOFactory` is a simple utility class for creating a PDO instance to connect to a MySQL database.
It uses a strongly typed `PDOConfig` object instead of an array to improve clarity, IDE support, and validation.

Installation
------------

[](#installation)

### Via Composer

[](#via-composer)

You can install `PDOFactory` via Composer. Run the following command:

```
composer require alaureati-enerds/pdofactory
```

Or add the dependency directly to your composer.json file and run composer install.

### Manual Installation

[](#manual-installation)

You can also manually include the `PDOFactory.php` and `PDOConfig.php` files in your project if you are not using Composer.

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

```
require 'vendor/autoload.php';

use PDOFactory\PDOFactory;
use PDOFactory\PDOConfig;

$config = new PDOConfig(
    dbHost: 'localhost',
    dbPort: 3306,
    dbName: 'my_database',
    dbUser: 'my_user',
    dbPass: 'my_password'
);

$pdo = PDOFactory::create($config);

$stmt = $pdo->query("SELECT * FROM users");
$results = $stmt->fetchAll();
print_r($results);
```

### Example with Additional PDO Options

[](#example-with-additional-pdo-options)

```
$config = new PDOConfig(
    dbHost: 'localhost',
    dbPort: 3306,
    dbName: 'my_database',
    dbUser: 'my_user',
    dbPass: 'my_password',
    dbCharset: 'utf8mb4',
    options: [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]
);

$pdo = PDOFactory::create($config);
```

### Creating Configuration from Environment Variables

[](#creating-configuration-from-environment-variables)

You can create a `PDOConfig` instance directly from environment variables using the static `fromEnv()` method.

Example:

```
$config = PDOConfig::fromEnv();
$pdo = PDOFactory::create($config);
```

This expects the following environment variables to be set:

```
DB_HOST=localhost
DB_PORT=3306
DB_NAME=my_database
DB_USER=my_user
DB_PASS=my_password
DB_CHARSET=utf8

```

You can load these variables from a `.env` file using [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv):

```
use Dotenv\Dotenv;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
```

### Creating Configuration from an Array

[](#creating-configuration-from-an-array)

You can also create a `PDOConfig` instance from an associative array using the `fromArray()` method.

Example:

```
$configArray = [
    'db_host' => 'localhost',
    'db_port' => 3306,
    'db_name' => 'my_database',
    'db_user' => 'my_user',
    'db_pass' => 'my_password',
    'db_charset' => 'utf8',
];

$config = PDOConfig::fromArray($configArray);
$pdo = PDOFactory::create($config);
```

This approach is useful when working with configuration files or injecting database settings at runtime.

### Error Handling

[](#error-handling)

The `create()` method throws a `PDOException` if the connection fails due to incorrect configuration or other database errors.
You can handle these exceptions as follows:

```
try {
    $pdo = PDOFactory::create($config);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
```

License
-------

[](#license)

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing
------------

[](#contributing)

Contributions are welcome! Please open an issue to discuss what you would like to change or submit a pull request with your improvements.

Author: Andrea Laureati -

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance57

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~18 days

Total

9

Last Release

286d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2aacb3989785a34a72e693f71a18a2c05bc56542849ce90ef9e5bcf55245488d?d=identicon)[alaureati-enerds](/maintainers/alaureati-enerds)

---

Top Contributors

[![alaureati-enerds](https://avatars.githubusercontent.com/u/127527027?v=4)](https://github.com/alaureati-enerds "alaureati-enerds (10 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alaureati-enerds-pdofactory/health.svg)

```
[![Health](https://phpackages.com/badges/alaureati-enerds-pdofactory/health.svg)](https://phpackages.com/packages/alaureati-enerds-pdofactory)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M543](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
