PHPackages                             gajus/doll - 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. gajus/doll

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

gajus/doll
==========

Extended PDO with inline type hinting, deferred connection support, logging and benchmarking.

1.2.0(11y ago)341627[4 issues](https://github.com/gajus/doll/issues)[1 PRs](https://github.com/gajus/doll/pulls)BSD-3-ClausePHPPHP &gt;=5.4

Since Jun 3Pushed 7y ago4 watchersCompare

[ Source](https://github.com/gajus/doll)[ Packagist](https://packagist.org/packages/gajus/doll)[ Docs](https://github.com/gajus/doll)[ RSS](/packages/gajus-doll/feed)WikiDiscussions master Synced today

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

Doll
====

[](#doll)

[![Build Status](https://camo.githubusercontent.com/dd4cc9ee8417d840c9232a8ad45cb8482854a3181a084dff256db981b31ca2e7/68747470733a2f2f7472617669732d63692e6f72672f67616a75732f646f6c6c2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/gajus/doll)[![Coverage Status](https://camo.githubusercontent.com/464c91776b3fac58a622e97f268b05004a422557ce57876a850bae09abc62715/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67616a75732f646f6c6c2f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/gajus/doll?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/4a3f1f8c5ddf7f87be2fed43116f0755ed8fa8f31bebae63941fe1ab4b300297/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f646f6c6c2f76657273696f6e2e706e67)](https://packagist.org/packages/gajus/doll)[![License](https://camo.githubusercontent.com/4167d045476acd90579a9cd5a383eece21f6ff13ce7803bf79d220b7376d7de0/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f646f6c6c2f6c6963656e73652e706e67)](https://packagist.org/packages/gajus/doll)

Extended PDO with inline type hinting, deferred connection support, logging and benchmarking.

Single Parameter Constructor
----------------------------

[](#single-parameter-constructor)

[PDO::\_\_construct](http://uk3.php.net/manual/en/pdo.construct.php) is using Data Source Name (DSN) string to describe the connection. PDO DSN implementation does not include user, password and driver options.

Doll instance is described using `DataSource` object:

```
$data_source = new \Gajus\Doll\DataSource([
    'host' => '127.0.0.1',
    'driver' => 'mysql',
    'database' => null,
    'user' => null,
    'password' => null,
    'charset' => 'utf8',
    'driver_options' => []
]);
```

Deferred Connection
-------------------

[](#deferred-connection)

PDO will establish a connection to the database upon initialization. If application initializes PDO during the bootstrap, but does not execute queries (e.g. request that is served from cache), the connection is unnecessary.

Doll will not connect to the database upon initialization:

```
$db = new \Gajus\Doll\PDO($data_source);
```

The connection is deferred until either of the following methods are invoked:

- [PDO::prepare()](http://php.net/manual/en/pdo.prepare.php)
- [PDO::exec()](http://php.net/manual/en/pdo.exec.php)
- [PDO::query()](http://php.net/manual/en/pdo.query.php)
- [PDO::beginTransaction()](http://php.net/manual/en/pdo.begintransaction.php)
- [PDO::commit()](http://php.net/manual/en/pdo.commit.php)
- [PDO::rollBack()](http://php.net/manual/en/pdo.rollback.php)
- [PDOStatement::execute()](http://php.net/manual/en/pdostatement.execute.php)

Default Attributes
------------------

[](#default-attributes)

AttributePDODoll`PDO::ATTR_ERRMODE``PDO::ERRMODE_SILENT``PDO::ERRMODE_EXCEPTION``PDO::ATTR_EMULATE_PREPARES``false``true``PDO::ATTR_DEFAULT_FETCH_MODE``PDO::FETCH_BOTH``PDO::FETCH_ASSOC``PDO::ATTR_STATEMENT_CLASS``PDOStatement``Gajus\Doll\PDOStatement`AttributeReason`PDO::ATTR_ERRMODE`Enables [method chaining](#method-chaining).`PDO::ATTR_EMULATE_PREPARES`[`PDO_MYSQL`](http://php.net/manual/en/ref.pdo-mysql.php) will take advantage of native prepared statement support present in MySQL 4.1 and higher. It will always [fall back](http://lt1.php.net/manual/en/pdo.setattribute.php) to emulating the prepared statement if the driver cannot successfully prepare the current query.`PDO::ATTR_DEFAULT_FETCH_MODE`More convenient.`PDO::ATTR_STATEMENT_CLASS`Required for the [extended type hinting](#extended-type-hinting) implementation.Values for attributes not in the table do not differ.

Method Chaining
---------------

[](#method-chaining)

[PDOStatement::execute()](http://www.php.net/manual/en/pdostatement.execute.php) returns a boolean value indicating the state of the transaction, e.g.

```
$sth = $db->prepare("SELECT ?"); // PDOStatement
$sth->execute([1]); // boolean
$input = $sth->fetch(PDO::FETCH_COLUMN);
```

However, if you are using [PDO::ERRMODE\_EXCEPTION](http://uk1.php.net/manual/en/pdo.error-handling.php) error handling strategy, the output of `execute` is redundant.

Doll forces `PDO::ERRMODE_EXCEPTION` error handling strategy, while `execute` method returns an instance of `\Gajus\Doll\PDOStatement`. This allows further method chaining, e.g.

```
$input = $db
    ->prepare("SELECT ?") // Gajus\Doll\PDOStatement
    ->execute([1]) // Gajus\Doll\PDOStatement
    ->fetch(PDO::FETCH_COLUMN);
```

Extended Type Hinting
---------------------

[](#extended-type-hinting)

### Inline Type Hinting

[](#inline-type-hinting)

[PDOStatement::bindValue()](http://php.net/manual/en/pdostatement.bindvalue.php) method allows to set the parameter type. However, the syntax is verbose:

```
$sth = $db->prepare("SELECT :foo, :bar, :baz");
$sth->bindValue('foo', 'foo', PDO::PARAM_STR);
$sth->bindValue('bar', 1, PDO::PARAM_INT);
$sth->bindValue('baz', $fp, PDO::PARAM_LOB);
$sth->execute();
```

Doll allows inline type hinting:

```
$sth = $db->prepare("SELECT s:foo, i:bar, l:baz");
$sth->execute(['foo' => 'foo', 'bar' => 1, 'baz' => $fp]);
```

Doll implementation supports all of the parameter types:

NameParameter Type`b``PDO::PARAM_BOOL``n``PDO::PARAM_NULL``i``PDO::PARAM_INT``s``PDO::PARAM_STR``l``PDO::PARAM_LOB`### Inferred Type Hinting

[](#inferred-type-hinting)

When parameter name is "id" or ends with "\_id", unless an explicit parameter type is set, Doll will use `PDO::PARAM_INT`, e.g.

```
$db->prepare("SELECT :id, :foo_id");
```

Is equivalent to:

```
$db->prepare("SELECT i:id, i:foo_id");
```

You can explicitly set the parameter type:

```
$db->prepare("SELECT s:id, s:foo_id");
```

You can disable the inferred type hinting:

```
$db->setAttribute(\Gajus\Doll\PDO::ATTR_INFERRED_TYPE_HINTING, false);
```

Parameter Marker Reuse
----------------------

[](#parameter-marker-reuse)

Using Doll, you can reuse the named parameter markers in your prepared statements, e.g.

```
$db->prepare("SELECT :foo, :foo");
```

The native PDO implementation does not support it. It will raise the following error:

> PDOException: SQLSTATE\[HY093\]: Invalid parameter number

Logging and Benchmarking
------------------------

[](#logging-and-benchmarking)

Doll supports query and statement execution logging. To enable logging, you need to set `\Gajus\Doll\PDO::ATTR_LOGGING` attribute to `true`.

```
$db->setAttribute(\Gajus\Doll\PDO::ATTR_LOGGING, true);

$db
    ->prepare("SELECT :foo, SLEEP(.2)")
    ->execute(['foo' => 'a']);

$log = $db->getLog();

var_dump($log);
```

The log output contains the following information about each query:

```
array(1) {
    [0]=>
        array(7) {
            ["statement"]=>
                string(22) "SELECT :foo, SLEEP(.2)"
            ["parameters"]=>
                array(1) {
                    ["foo"]=>
                        string(1) "a"
                }
            ["execution_wall_time"]=>
                float(0.20117211341858)
            ["backtrace"]=>
                array(5) {
                    ["file"]=>
                        string(85) "/../doll/tests/LogTest.php"
                    ["line"]=>
                        int(28)
                    ["function"]=>
                        string(7) "execute"
                    ["class"]=>
                        string(23) "Gajus\Doll\PDOStatement"
                    ["type"]=>
                        string(2) "->"
                }
            ["execution_duration"]=>
                float(0.200723)
            ["execution_overhead"]=>
                float(0.00044911341857909)
            ["query"]=>
                string(19) "SELECT ?, SLEEP(.2)"
    }
}
```

"execution\_duration" and "query" are retrieved from [SHOW PROFILES](http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html). Doll will automatically run diagnostics every 100 executions to overcome the [limit of 100 queries](http://dev.mysql.com/doc/refman/5.6/en/show-profile.html).

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% 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 ~55 days

Recently: every ~68 days

Total

6

Last Release

4083d ago

PHP version history (2 changes)1.0.0PHP &gt;=5.5

1.1.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b46b18abe058b038c677a9fa4aa6c39de7597196fb72a11add1f11600412a0e5?d=identicon)[gajus](/maintainers/gajus)

---

Top Contributors

[![gajus](https://avatars.githubusercontent.com/u/973543?v=4)](https://github.com/gajus "gajus (87 commits)")[![colshrapnel](https://avatars.githubusercontent.com/u/2895470?v=4)](https://github.com/colshrapnel "colshrapnel (1 commits)")

---

Tags

databasepdo

### Embed Badge

![Health badge](/badges/gajus-doll/health.svg)

```
[![Health](https://phpackages.com/badges/gajus-doll/health.svg)](https://phpackages.com/packages/gajus-doll)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[tommyknocker/pdo-database-class

Framework-agnostic PHP database library with unified API for MySQL, MariaDB, PostgreSQL, SQLite, MSSQL, and Oracle. Query Builder, caching, sharding, window functions, CTEs, JSON, migrations, ActiveRecord, CLI tools, AI-powered analysis. Zero external dependencies.

845.7k](/packages/tommyknocker-pdo-database-class)

PHPackages © 2026

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