Type safe getters - phpgt/Input GitHub Wiki

All request data arrives over HTTP as text, but application code usually expects something more specific. Input and InputData both provide type-safe getters so we can read values in the type our code expects.

Available getters

Single values

  • getString(string $key): ?string
  • getInt(string $key): ?int
  • getFloat(string $key): ?float
  • getBool(string $key): ?bool
  • getDateTime(string $key, ?string $format = null): ?DateTimeInterface
  • getFile(string $key): ?FileUpload

Multiple values

  • getMultipleString(string $key): array
  • getMultipleInt(string $key): array
  • getMultipleFloat(string $key): array
  • getMultipleBool(string $key): array
  • getMultipleDateTime(string $key): array
  • getMultipleFile(string $key): array

Nulls and empty values

Single-value getters return null when the key does not exist.

For numeric, boolean, and date getters, an empty string is also treated as null. That is useful for optional form fields where an empty control should not become 0, false, or an invalid date by accident.

Strings, integers, floats, and booleans

$name = $input->getString("name");
$age = $input->getInt("age");
$height = $input->getFloat("height");
$termsAccepted = $input->getBool("accept-terms");

getBool() uses normal PHP boolean casting rules. For example, a non-empty string becomes true. If we need stricter semantics such as accepting only "true" and "false", read the value as a string and validate it explicitly in application code.

For multiple-value controls, use the matching getMultiple* getter:

<label><input type="checkbox" name="topping[]" value="mozzarella" /> Mozzarella</label>
<label><input type="checkbox" name="topping[]" value="basil" /> Basil</label>
<label><input type="checkbox" name="topping[]" value="olive" /> Olive</label>
foreach($input->getMultipleString("topping") as $topping) {
	$pizza->addTopping($topping);
}

PHP requires the square brackets in the field name for repeated values. Without [], PHP will not treat the submitted values as a list.

Dates and times

getDateTime() constructs a DateTimeInterface from the input value:

$published = $input->getDateTime("published-at");

If the incoming value has a fixed format, we can supply it explicitly:

$date = $input->getDateTime("start-date", "Y-m-d");

If a date string exists but cannot be parsed into the requested format, the library throws DataNotCompatibleFormatException. That makes invalid date input visible to application code rather than silently producing the wrong value.

Files

getFile() returns a FileUpload object, or null if that file field was not submitted.

$avatar = $input->getFile("avatar");
if($avatar) {
	echo $avatar->getOriginalName();
}

If the form field exists in the body but was sent as plain text rather than as a file upload, getFile() throws DataNotFileUploadException. That usually means the form is missing enctype="multipart/form-data".

Choosing the right getter

  • Use getString() when the value is free-form text or when you want to validate the format yourself.
  • Use getInt() and getFloat() for numeric fields that should already have been constrained by the form or by server-side validation.
  • Use getBool() when the input represents a true/false choice and you want PHP truthiness semantics.
  • Use getDateTime() when the value should become a real date object immediately.
  • Use getMultiple*() for checkbox groups, multi-selects, and repeated file inputs.

Next: Reading request data and streams

⚠️ **GitHub.com Fallback** ⚠️