Back to Groups
Development

PHP: web persistence engineer

PHP 8.2 standard library and PDO. Parse request values and wrap prepared database work in explicit transactions. Review names and integration points before use.

workflowsphp-developerpdocodephp
AL
alex.t
0likes12snippets

Try it here

Loading snippet…

Your typing stays in this tab. Nothing is saved or sent.
Snippets (12)
;php-json-object
Decode a bounded JSON object
function decodeJsonObject(string $json): stdClass { if (strlen($json) > 1048576) { throw
;php-pdo-read
Prepared lookup with a bound integer ID
function findProject(PDO $db, int $id): ?array { if ($id < 1) { throw new InvalidArgumen
;php-date
Reject invalid or rolled-over calendar dates
function parseCalendarDate(string $input): DateTimeImmutable { if (!preg_match('/\A\d{4}-\d{2}-\
;php-html
Escape text for an HTML context
function escapeHtml(string $value): string { return htmlspecialchars( $value, EN
;php-transaction
Own a PDO transaction and roll back on failure
function inTransaction(PDO $db, callable $operation): mixed { if ($db->inTransaction()) {
;php-chunks
Yield fixed-size batches from an iterable
function iterableChunks(iterable $items, int $size): Generator { if ($size < 1) { throw
;php-bool-setting
Parse a string boolean without truthiness traps
function parseBoolSetting(?string $raw): bool { if ($raw === null || trim($raw) === '') {
;php-page-number
Validate a request page number
function pageNumber(mixed $raw, int $maximum = 100000): int { if ($maximum < 1) { throw
;php-pdo-version
Update a project only at the expected version
function renameProject(PDO $db, int $id, int $version, string $name): bool { if ($db->getAttribu
;php-pdo-many
Fetch selected project IDs with bound placeholders
function findProjects(PDO $db, array $ids): array { if ($db->getAttribute(PDO::ATTR_ERRMODE) !==
;php-pdo-cursor
Read a project page by increasing ID
function projectPage(PDO $db, int $afterId = 0, int $size = 50): array { if ($db->getAttribute(P
;php-patch-fields
Validate editable project fields before persistence
function projectPatch(array $input): array { if ($input === [] || array_diff(array_keys($input),