Skip to content
Greg Bowler edited this page Apr 14, 2026 · 5 revisions

Encapsulated and type-safe user input.

The default behaviour of PHP user input is handled globally via superglobals such as $_GET, $_POST, $_FILES, etc. While this approach provides a straightforward method for simple user input data handling, it often encounters limitations, particularly when managing HTTP requests involving multiple fields or files.

This library wraps request input in objects so we can pass around only the data we actually need. It gives us one place to read query string values, form fields, uploaded files, raw request bodies, and JSON payloads, with type-safe getters for the common cases.

Tip

After integrating this library, it is recommended to prevent access to superglobals in PHP - see PHP.GT/ProtectedGlobal for an automated solution.

You may want to validate user input on the server - see PHP.GT/DomValidation for an automated solution.

This library and associated libraries are all part of PHP.GT/WebEngine.

Quick example

<form method="post" enctype="multipart/form-data">
	<label>
		<span>Name</span>
		<input name="name" required />
	</label>

	<label>
		<span>Age</span>
		<input type="number" name="age" />
	</label>

	<label>
		<span>Interests</span>
		<select name="interest[]" multiple>
			<option>Mathematics</option>
			<option>Cryptography</option>
			<option>Information Security</option>
			<option>Cyberwarfare</option>
		</select>
	</label>

	<label>
		<span>Photo</span>
		<input type="file" name="photo" />
	</label>

	<button name="do" value="save">Save profile</button>
</form>
use GT\Input\Input;
use GT\Input\InputData\Datum\FailedFileUpload;

function do_save(Input $input, ProfileStore $profiles, int $profileId):void {
	$profiles->update(
		$profileId,
		$input->getString("name"),
		$input->getInt("age"),
	);

	foreach($input->getMultipleString("interest") as $interest) {
		$profiles->addInterest($profileId, $interest);
	}

	$photo = $input->getFile("photo");
	if($photo instanceof FailedFileUpload) {
		throw new RuntimeException($photo->getErrorMessage());
	}

	if($photo) {
		$photo->moveTo("data/upload/$profileId." . $photo->getOriginalExtension());
	}
}

For projects that don't use WebEngine, or just to familiarise yourself with the library, see constructing the Input class.

Clone this wiki locally