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

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

uuf6429/php-castable
====================

Type casting functionality for PHP

2.0.0(2y ago)07MITPHPPHP ^8

Since Apr 23Pushed 2y ago1 watchersCompare

[ Source](https://github.com/uuf6429/php-castable)[ Packagist](https://packagist.org/packages/uuf6429/php-castable)[ RSS](/packages/uuf6429-php-castable/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

🎭 PHP Castable
==============

[](#-php-castable)

[![CI](https://github.com/uuf6429/php-castable/actions/workflows/ci.yml/badge.svg)](https://github.com/uuf6429/php-castable/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/440e8a378524ec500242afa75745ea8d37d3dde5d9f2b3365d3588540e9e0a91/68747470733a2f2f636f6465636f762e696f2f67682f757566363432392f7068702d6361737461626c652f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/uuf6429/php-castable)[![Minimum PHP Version](https://camo.githubusercontent.com/4be5d8f7174ca846ad97659d9490f12e734d59233f0480ec22b0e5b794c13951/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/3139d480b58231f359ab1938de1a808c57eb28f96e4288e81bb236f915b3dac3/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f7068702d6361737461626c652f6c6963656e7365)](https://packagist.org/packages/uuf6429/php-castable)[![Latest Stable Version](https://camo.githubusercontent.com/2948fd2f77809ccdc74aa7aae0887e150458ef6b81b786b99b60f3daa38ba065/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f7068702d6361737461626c652f76)](https://packagist.org/packages/uuf6429/php-castable)[![Latest Unstable Version](https://camo.githubusercontent.com/d4260eedfb76740f9e1660e94fb0a09756d2ee4510ea68cf161349a6451d7bbb/68747470733a2f2f706f7365722e707567782e6f72672f757566363432392f7068702d6361737461626c652f762f756e737461626c65)](https://packagist.org/packages/uuf6429/php-castable)

Basic groundwork for type-casting in PHP.

🔌 Installation
--------------

[](#-installation)

The recommended and easiest way to install this library is through Composer:

```
composer require uuf6429/php-castable
```

⭐️ Features / Functionality
---------------------------

[](#️-features--functionality)

- Works with simple types and objects
- `cast($value, $type)` function that converts a value to a target type.
- `Castable` interface, exposes method that is called whenever `cast()` is called on objects implementing this interface.
- Error handling - all errors routed to `NotCastableException`.
- Fixes type-hinting for IDEs understanding PHPDoc Generics.

While `cast()` is just a regular PHP function, it would be the equivalent to type-casting operators in other languages (e.g. `val as Type`, `(Type)val`, `val.to(Type)`, `CAST(val, TYPE)`...).

🚀 Example
---------

[](#-example)

```
class Cat implements \uuf6429\Castable\Castable
{
    public function castTo($type)
    {
        if ($type === Dog::class) {
            return new Dog();
        }

        throw new RuntimeException("Unsupported type $type.");
    }
}

class Dog {}

$dog = \uuf6429\Castable\cast(new Cat(), Dog::class); // ok, cat becomes a dog :)
$cat = \uuf6429\Castable\cast($dog, Cat::class);      // not allowed
```

🔍 Casting Behaviour
-------------------

[](#-casting-behaviour)

The casting process follows these steps:

1. If the value is an object or value of the desired type, then it is returned unchanged.
2. If the value is an *object* that *implements `Castable` interface*, `castTo()` is called and its value returned.
3. Otherwise, PHP's `settype()` is attempted.

At any point in time, errors or unsupported type-casting could occur, in which case a `NotCastableException` is thrown.

💰 Motivation
------------

[](#-motivation)

In many cases, having specific `castToX()` methods in your classes is enough, and it typically works adequately.

However, this could get very repetitive and somewhat error-prone, until a more dynamic solution is needed. This package helps to safely avoid all that boilerplate code.

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

Total

4

Last Release

761d ago

Major Versions

0.0.2 → 1.0.02022-04-23

1.0.0 → 2.0.02024-06-02

PHP version history (2 changes)0.0.1PHP ^5.6 || ^7 || ^8

2.0.0PHP ^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/450767af6ef832ad662c169bf718d6d25c025c08b2d91b810959d190bccebba1?d=identicon)[uuf6429](/maintainers/uuf6429)

---

Top Contributors

[![uuf6429](https://avatars.githubusercontent.com/u/230049?v=4)](https://github.com/uuf6429 "uuf6429 (6 commits)")

---

Tags

castingphpphp-librarytypesuuf6429

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[siteation/magento2-storeinfo-menus

Get your store menus with ease

102.3k](/packages/siteation-magento2-storeinfo-menus)

PHPackages © 2026

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