Concept demo, not a library. This repository exists to be read. It shows one way to structure raw-SQL database access in PHP so that it stays type-safe, testable and checked against the real schema. It is deliberately small and heavily commented. Do not
composer requireit. Copy the ideas, or the files, into your own project and adapt them.
It accompanies the article SQL-First PHP: Query Objects, Typed Rows and a Database That Checks Your SQL, which explains the reasoning in more depth.
There is no ORM and no query builder. Every SQL statement is written out in full as a
public const string SQL on its own small class. Every statement goes through one thin,
mockable gateway interface. Rows are read through a typed, fail-fast reader, never (int)$row['x'].
Column names are written down once, in a hand-written table class, and everything else refers to
them through it. Then the build makes those conventions true rather than hoped for: three PHPStan
rules ban the shortcuts, and two real-database checks have MySQL itself confirm that the table
class matches the table and that every SQL constant prepares.
| # | File | What it shows |
|---|---|---|
| 1 | src/Db/DbServiceInterface.php |
The whole gateway surface: six methods, parameters always bound |
| 2 | example/Product/FindProductByIdQuery.php |
A query class: one SQL constant, one typed method |
| 3 | example/Product/ProductTable.php |
The table class: names and types written down once |
| 4 | example/Product/Product.php |
The DTO, hydrated with no literal column names |
| 5 | src/Result/Row.php |
The typed row reader: exact decimals, no silent zeroes |
| 6 | tests/Product/FindProductByIdQueryTest.php |
Unit tier, against the fake (configured by SQL text) |
| 7 | src/Testing/FakeDbService.php |
The fake, and why it is not a mock |
| 8 | tests/Product/FindProductByIdQueryIntegrationTest.php |
Real-database tier, same query, throwaway schema |
| 9 | example/Product/AdjustStockStmt.php |
A statement class: construct once, run many times |
| 10 | example/Product/CreateProductCommand.php |
Writes through Insert::row(), the one composed statement |
| 11 | example/Product/CountProductsByStatusQuery.php |
An aggregate, with a result class for its aliases |
| 12 | src/Db/PdoDbService.php |
The real gateway: retry (and when not to), statement cache, logging |
| 13 | phpstan/Rules/ |
The three rules that turn conventions into build failures |
| 14 | tests/Product/ProductTableTest.php |
Check one: table class versus information_schema |
| 15 | tests/Checks/SqlConstantsPrepareTest.php |
Check two: MySQL prepares every SQL constant in the app |
src/ is the small reusable core. example/ is "your application": one product table, built
from example/schema.sql.
composer install
composer stan # PHPStan, level max, with the three custom rules
composer test # unit tests, no database needed (real-db tests are skipped)The real-database tests need a MySQL 8 (MariaDB works too) where the user can create and drop
databases named test_demo_*. Each test makes its own throwaway schema and drops it afterwards.
Do not point this at a server holding data you care about.
export DEMO_DB_DSN='mysql:host=127.0.0.1;port=3306'
export DEMO_DB_USER=demo
export DEMO_DB_PASSWORD=demo
composer test-dbCI (.github/workflows/ci.yml) runs all of it against MySQL 8.4, with
--fail-on-skipped so the real-database tier cannot pass by quietly not running.
A real project built on this pattern needs more than this demo carries. Left out to keep the point visible:
- Migrations.
example/schema.sqlstands in for them. - Framework wiring. In Symfony or Laravel you would register
PdoDbServiceas theDbServiceInterfaceimplementation and exclude query classes from autowiring. - More column types. Dates, JSON, floats and unsigned big integers follow the same shape as the ones here.
- Bulk writes, streaming exports, multiple connections, read replicas. All fit the same gateway; none change the idea.
PHP 8.4+, ext-pdo_mysql. MIT licensed.