PHPackages                             improved/type - 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. improved/type

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

improved/type
=============

Type handling

v0.1.1(7y ago)4210.0k↓39.7%23MITPHPPHP &gt;=7.2.0

Since Oct 30Pushed 4y agoCompare

[ Source](https://github.com/improved-php-library/type)[ Packagist](https://packagist.org/packages/improved/type)[ RSS](/packages/improved-type/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (3)

[![improved PHP library](https://user-images.githubusercontent.com/100821/46372249-e5eb7500-c68a-11e8-801a-2ee57da3e5e3.png)](https://user-images.githubusercontent.com/100821/46372249-e5eb7500-c68a-11e8-801a-2ee57da3e5e3.png)

type handling
=============

[](#type-handling)

[![PHP](https://github.com/improved-php-library/type/actions/workflows/php.yml/badge.svg)](https://github.com/improved-php-library/type/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c8d9a6f34c137875c90b99ed2b9710243e1ffa3b78f786c1e81829df645a4303/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d70726f7665642d7068702d6c6962726172792f747970652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/improved-php-library/type/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/306fb4810a08a537866d39613f700a503790fd255ec9105b3b5cbcbe37eb6be0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d70726f7665642d7068702d6c6962726172792f747970652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/improved-php-library/type/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/e9e3ac62c77cff7357b4c8542bb0dd84f937a6004a9837cd1a6c96f0ab572195/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d70726f7665642f747970652e737667)](https://packagist.org/packages/improved/type)[![Packagist License](https://camo.githubusercontent.com/cd9163e6b361c1b35d7736ede4adb121359790988efbfb610be3becdcb7e2c23/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f696d70726f7665642f747970652e737667)](https://packagist.org/packages/improved/type)

Library for type handling.

This library provides a set of consistent functions for PHP. You should always use these types rather than the ones provided by PHP natively.

Installation
------------

[](#installation)

```
composer require improved/type

```

types
-----

[](#types)

- [`type_is(mixed $var, string|string[] $type)`](#type_is)
- [`type_check(mixed $var, string|string[] $type, Throwable $throwable = null)`](#type_check)
- [`type_cast(mixed $var, string|string[] $type, Throwable $throwable = null)`](#type_cast)
- [`type_describe(mixed $var)`](#type_describe)

Reference
---------

[](#reference)

### type\_is

[](#type_is)

```
bool type_is(mixed $var, string|string[] $type)

```

Return true if variable has the specified type.

As type you can specify any internal type, including `callable` and `object`, a class name or a resource type (eg `stream resource`).

Typed arrays or iteratators are **not** supported, as it would require looping through them.

A question mark can be added to a class to accept null, eg `"?string"` is similar to using `["string", "null"]`.

### type\_check

[](#type_check)

```
mixed type_check(mixed $var, string|string[] $type, Throwable $throwable = null)

```

Validate that a variable has a specific type. The same types cas in `type_is()` can be used.

Typed arrays or iteratators are **not** supported. Use [`iterable_check_type`](https://github.com/improved-php-library/iterable#checktype) instead.

By default a `TypeError` is thrown. Optionally you can pass an exception instead.

The message may contain a `%s`, which is replaced by the type description of `$var`. It optionally may contain a second `%s` which is replaced by the description of the required type. Use sprintf positioning to use the required type first.

If the message or the exception or error contains a `%`, a new throwable of the same type is created with the filled out message.

The function returns `$var` if the type matches, so you can use it while setting a variable.

```
use Improved as i;

$date = i\type_check(get_some_date(), DateTimeInterface::class);
$name = i\type_check($user->getName(), 'string');
$number = i\type_check(get_distance(), ['int', 'float']);

$foo = i\type_check(do_something(), Foo::class, new UnexpectedException('Wanted %2$s, not %1$s'));
```

A question mark can be added to a class to accept null, eg `"?string"` is similar to using `["string", "null"]`.

### type\_cast

[](#type_cast)

```
mixed type_cast(mixed $var, string $type, Throwable $throwable = null)

```

Check the variable has a specific type or can be casted to that type, otherwise throw a `TypeError` or exception.

This function is similar to `type_check`, with the difference that is will cast the value in some cases

fromto`string``int`only numeric strings and &lt; `PHP_INT_MAX``string``float`only numeric strings`int``bool`only 0 or 1`int``float``int``string``float``int`if float &lt; `PHP_INT_MAX``float``string``bool``int``array``object` \\ `stdClass`if array has no numeric keys`object``string`if only has `__toString()` method`object``array` \\ `iterable`only `stdClass` objects`null`any scalar`null``array``null``object` \\ `stdClass`In contrary to `type_is` and `type_check`, only one type may be specified.

A question mark can be added to a class to accept null, eg `?string` will try to cast everything to a string except `null`.

### type\_describe

[](#type_describe)

```
string type_describe(mixed $var, bool $detailed = false)

```

Get the type of a variable in a descriptive way to using in an (error) message.

For scalar, null and array values, the result is the same a `gettype`. Except for floats which will return `float`rather than `double`.

Objects are have their class name, appended with `object`. For resources the resource type is returned, appended with `resource`.

```
type_describe('hello');        // string
type_describe(22);             // integer
type_describe(STDIN);          // stream resource
type_describe(new DateTime()); // instance of DateTime
```

The detailed option describes both the value an type. This is similar to what is used in the error messages of `type_check` and `type_cast`.

```
type_describe('hello');         // string(5) "hello"
type_describe(22);              // int(22)
type_describe(["a", "b", "c"]); // array(3)
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 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 ~113 days

Total

2

Last Release

2644d ago

PHP version history (2 changes)v0.1.0PHP &gt;=7.1.0

v0.1.1PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (23 commits)")

### Embed Badge

![Health badge](/badges/improved-type/health.svg)

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

###  Alternatives

[heptacom/heptaconnect-portal-base

HEPTAconnect base dataset that every other portal is based on

1024.9k13](/packages/heptacom-heptaconnect-portal-base)

PHPackages © 2026

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