PHPackages                             php-cast/php-cast - 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. php-cast/php-cast

ActiveLibrary

php-cast/php-cast
=================

Cast variables to different types. Native types, generic types, custom classes, nested classes, and more

v2.1.0(3y ago)31.1k—0%1MITPHPPHP ^7.4|^8.0

Since May 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/JacobRothDevelopment/PHP-Cast)[ Packagist](https://packagist.org/packages/php-cast/php-cast)[ Docs](https://github.com/JacobRothDevelopment/PHP-Cast)[ RSS](/packages/php-cast-php-cast/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (4)Used By (1)

PHP-Cast
========

[](#php-cast)

Cast variables to different types. Native types, generic types, custom classes, nested classes, and more

Install
=======

[](#install)

```
composer require php-cast/php-cast
```

Usage
=====

[](#usage)

```
// Example Classes and Data
class Item1
{
    public int $id;
}

class Item2
{
    public int $id;
    public string $name;
    public ?int $nullableInt;
}

class ItemCollective
{
    public Item1 $item1;
    public Item2 $item2;
}

$i1_arr = [
    "id" => 1,
];

$i1_arr_str_id = [
    "id" => "11",
];
$i2_arr = [
    "id" => 2,
    "name" => "name value",
];

$ic_arr = [
    "item1" => $i1_arr,
    "item2" => $i2_arr,
];
```

```
// Usage Examples of PhpCast

\PhpCast\Cast::cast("Item1", $i1_arr);
/* Item1 Object
(
    [id] => 1
) */

\PhpCast\Cast::cast("Item2", $i2_arr);
/* Item2 Object
(
    [id] => 2
    [name] => name value
    [nullableInt] =>
) */

\PhpCast\Cast::cast("ItemCollective", $ic_arr);
/* ItemCollective Object
(
    [item1] => Item1 Object
        (
            [id] => 1
        )

    [item2] => Item2 Object
        (
            [id] => 2
            [name] => name value
            [nullableInt] =>
        )

) */

\PhpCast\Cast::cast("Item1", $i2_arr);
/* Item1 Object
(
    [id] => 2
) */

\PhpCast\Cast::cast("Item1", $i1_arr_str_id);
/* Item1 Object
(
    [id] => 11
) */

\PhpCast\Cast::cast("int", 9.234);
// 9

\PhpCast\Cast::cast("int", 9.234, true); // TypeError - input is not of type "int"
\PhpCast\Cast::cast("Item2", $i1_arr); // TypeError - non-nullable property "name"
\PhpCast\Cast::cast("Item1", $i1_arr_str_id, true); // TypeError - property Item1::id is of type "int"
```

See more usage cases in `tests/`. Full and up-to-date examples will be available at `https://github.com/JacobRothDevelopment/PHP-Cast/tree/main/tests`

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

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

Total

3

Last Release

1296d ago

Major Versions

v1.0.0 → v2.0.02022-05-15

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/53883026?v=4)[JacobRothDevelopment](/maintainers/JacobRothDevelopment)[@JacobRothDevelopment](https://github.com/JacobRothDevelopment)

---

Top Contributors

[![JacobRothDevelopment](https://avatars.githubusercontent.com/u/53883026?v=4)](https://github.com/JacobRothDevelopment "JacobRothDevelopment (17 commits)")

---

Tags

castcustom classnested class

### Embed Badge

![Health badge](/badges/php-cast-php-cast/health.svg)

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

###  Alternatives

[webpatser/laravel-uuid

Laravel integration for webpatser/uuid - High-performance drop-in UUID replacements (15% faster than Ramsey). Provides Str macros, HasUuids trait, facades, and casts. RFC 4122/9562 compliant.

1.8k17.3M129](/packages/webpatser-laravel-uuid)[vkovic/laravel-custom-casts

Make your own custom cast type for Laravel model attributes

218362.7k3](/packages/vkovic-laravel-custom-casts)[esensi/model

The base model traits of Esensi

20266.5k1](/packages/esensi-model)[henzeb/enumhancer

Your framework-agnostic Swiss Army knife for PHP 8.1+ native enums

69287.4k2](/packages/henzeb-enumhancer)[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)

PHPackages © 2026

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