PHPackages                             phpgt/typesafegetter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpgt/typesafegetter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpgt/typesafegetter
====================

An interface for objects that expose type-safe getter methods.

v1.3.4(2mo ago)026.6k↑71.8%[1 issues](https://github.com/PhpGt/TypeSafeGetter/issues)6MITPHPPHP &gt;=8.0CI passing

Since Jan 16Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/PhpGt/TypeSafeGetter)[ Packagist](https://packagist.org/packages/phpgt/typesafegetter)[ GitHub Sponsors](https://github.com/sponsors/PhpGt)[ RSS](/packages/phpgt-typesafegetter/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (12)Versions (19)Used By (6)

An interface for objects that expose type-safe getter methods.
==============================================================

[](#an-interface-for-objects-that-expose-type-safe-getter-methods)

Throughout PHP.GT repositories, wherever an object represents enclosed data, a consistent interface is used to expose the data in a type-safe manner.

---

[ ![Build status](https://camo.githubusercontent.com/8d0e5f5305af3a2a1fcad83de25f08503d8a10a225af8d2e900d7151b5416fb9/68747470733a2f2f62616467652e7374617475732e7068702e67742f74797065736166656765747465722d6275696c642e737667)](https://github.com/PhpGt/TypeSafeGetter/actions)[ ![Code quality](https://camo.githubusercontent.com/6c5bccd57740744c324836a4a64843ce8ffee057c4af028f9ab09da3b9bb4a5f/68747470733a2f2f62616467652e7374617475732e7068702e67742f74797065736166656765747465722d7175616c6974792e737667)](https://app.codacy.com/gh/PhpGt/TypeSafeGetter)[ ![Code coverage](https://camo.githubusercontent.com/ab5b84edaf566fd092ab380877928c427a7510aea09e7c77364a64fcd50bb1e3/68747470733a2f2f62616467652e7374617475732e7068702e67742f74797065736166656765747465722d636f7665726167652e737667)](https://app.codecov.io/gh/PhpGt/TypeSafeGetter)[ ![Current version](https://camo.githubusercontent.com/70a36ca248c71e5f8dd014e8cd4ac90c8a5e7b0be58a94f0bc907c49816a56bc/68747470733a2f2f62616467652e7374617475732e7068702e67742f74797065736166656765747465722d76657273696f6e2e737667)](https://packagist.org/packages/PhpGt/TypeSafeGetter)[ ![PHP.GT/TypeSafeGetter documentation](https://camo.githubusercontent.com/0d9924c131f56f6eb4fd5e288bcb8d7eb8a07d5930d06c79cebcec9ebaf9d724/68747470733a2f2f62616467652e7374617475732e7068702e67742f74797065736166656765747465722d646f63732e737667)](https://www.php.gt/typesafegetter)This package defines:

- `GT\TypeSafeGetter\TypeSafeGetter`
- `GT\TypeSafeGetter\NullableTypeSafeGetter`
- `GT\TypeSafeGetter\CallbackTypeSafeGetter`

The usual pattern is to implement `get()` yourself and use the `NullableTypeSafeGetter` trait for the common typed methods.

```
use DateTimeImmutable;
use DateTimeInterface;
use GT\TypeSafeGetter\NullableTypeSafeGetter;
use GT\TypeSafeGetter\TypeSafeGetter;

class DataStore implements TypeSafeGetter {
	use NullableTypeSafeGetter;

	public function __construct(
		private readonly array $data = [],
	) {}

	public function get(string $name):mixed {
		return $this->data[$name] ?? null;
	}
}

$store = new DataStore([
	"id" => "42",
	"active" => 1,
	"created" => "2024-05-01 09:15:00",
]);

echo $store->getInt("id");
var_dump($store->getBool("active"));
echo $store->getDateTime("created")?->format("Y-m-d");
```

`getInstance()` returns an existing object after checking its type:

```
$store = new DataStore([
	"date" => new DateTimeImmutable("2024-05-01"),
]);

$date = $store->getInstance("date", DateTimeInterface::class);
```

`getDateTime()` accepts:

- `DateTimeInterface` instances
- Unix timestamps
- date/time strings supported by `DateTimeImmutable`

The following methods are defined by this interface:

- `get(string $name):mixed` - A non-type-safe getter, used for getting keys that are not of an inbuilt type
- `getString(string $name):?string`
- `getInt(string $name):?int`
- `getFloat(string $name):?float`
- `getBool(string $name):?bool`
- `getDateTime(string $name):?DateTimeInterface`
- `getInstance(string $name, class-string $className):?T`

Common areas you will see this interface used:

- Database rows
- User input (from the query string or posted form data)
- Session and cookie storage
- HTTP header collections
- Configuration objects
- PHP.GT's [DataObject](https://www.php.gt/dataobject) repository.

`NullableTypeSafeGetter` trait
------------------------------

[](#nullabletypesafegetter-trait)

A lot of repositories within PHP.GT that utilise this class were repeating the same getter code, so this trait was introduced to remove the repetition. All getter functions of the interface are implemented, introducing a protected helper function `getNullableType` which removes the repetition of checking null values before casting them. The `getNullableType` function also allows a callback to be passed as the type parameter, allowing more complex nullable types to be constructed.

`CallbackTypeSafeGetter` interface
----------------------------------

[](#callbacktypesafegetter-interface)

This interface is for callback-driven lookups where a value may need to be fetched or computed as part of the read operation, such as cache APIs.

Unlike `NullableTypeSafeGetter`, there is no trait implementation in this package for the callback interface. Implementing classes define their own callback behaviour.

Proudly sponsored by
====================

[](#proudly-sponsored-by)

[JetBrains Open Source sponsorship program](https://www.jetbrains.com/community/opensource/)

[![JetBrains logo.](https://camo.githubusercontent.com/b5639e7738c6dfae9fe3f3e20175570b7376ce2577a772e09c25c2d4f14bf86e/68747470733a2f2f7265736f75726365732e6a6574627261696e732e636f6d2f73746f726167652f70726f64756374732f636f6d70616e792f6272616e642f6c6f676f732f6a6574627261696e732e737667)](https://www.jetbrains.com/community/opensource/)

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~193 days

Recently: every ~275 days

Total

11

Last Release

61d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9e42344b91ce4b91ab57875969f67a0a6a48de570a08bc65d673b06b72fd3a3f?d=identicon)[g105b](/maintainers/g105b)

---

Top Contributors

[![g105b](https://avatars.githubusercontent.com/u/358014?v=4)](https://github.com/g105b "g105b (11 commits)")

---

Tags

data-transfer-objectgetter-functionsinterfaceno-aitype-safety

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgt-typesafegetter/health.svg)

```
[![Health](https://phpackages.com/badges/phpgt-typesafegetter/health.svg)](https://phpackages.com/packages/phpgt-typesafegetter)
```

###  Alternatives

[carbon/includeassets

Include your assets (css, js) in an easy way into Neos

14235.7k15](/packages/carbon-includeassets)[components/modernizr

Modernizr is a JavaScript library that detects HTML5 and CSS3 features in the user's browser.

10256.1k8](/packages/components-modernizr)[rogervila/provably-fair

PHP implementation of Bustabit's Provably Fair system

1412.9k](/packages/rogervila-provably-fair)[eclipxe/sepomexphp

Servicio Postal Mexicano PHP Library (Unofficial)

101.1k](/packages/eclipxe-sepomexphp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
