Skip to content

Type safe getters

Greg Bowler edited this page Mar 3, 2026 · 5 revisions

In this section, we will fetch single-column results as concrete PHP types.

Single-value helpers

  • fetchBool()
  • fetchString()
  • fetchInt()
  • fetchFloat()
  • fetchDateTime()
$username = $db->fetchString("user/getNameById", 42);
$score = $db->fetchFloat("score/getAverage", 42);
$createdAt = $db->fetchDateTime("user/getCreatedAt", 42);

Multi-value helpers

  • fetchAllBool()
  • fetchAllString()
  • fetchAllInt()
  • fetchAllFloat()
  • fetchAllDateTime()
$ids = $db->fetchAllInt("user/getAllIds");

Type-safe access on Row

When we use fetch() or iterate fetchAll(), each row supports:

  • getString($column)
  • getInt($column)
  • getFloat($column)
  • getBool($column)
  • getDateTime($column)
  • get($column)
  • asArray()
  • property access such as $row->name

getDateTime() accepts date strings and numeric unix timestamps.

Example code: example/03-type-safe-getters.php

Clone this wiki locally