PHPackages                             sj-i/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. sj-i/php-cast

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

sj-i/php-cast
=============

Explicit cast with the behavior of implicit weak mode cast

v1.0.0(5y ago)5389.4k↑85.7%12MITPHPPHP ^7.0 || ^8.0

Since Jul 6Pushed 5y ago1 watchersCompare

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

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

CAST IN THE NAME OF PHP, YE NOT GUILTY
======================================

[](#cast-in-the-name-of-php-ye-not-guilty)

[![Minimum PHP version: 7.0.0](https://camo.githubusercontent.com/9aec0e8b9501927bfa3d8d40f6940f6ac71d75d2e4d5df9e83499bf42694a685/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302e302532422d626c75652e737667)](https://camo.githubusercontent.com/9aec0e8b9501927bfa3d8d40f6940f6ac71d75d2e4d5df9e83499bf42694a685/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302e302532422d626c75652e737667)[![Packagist](https://camo.githubusercontent.com/5c16802813353c4925e3c0e24839014cf19ed236f6885a7add3b2bea6024169e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736a2d692f7068702d636173742e737667)](https://packagist.org/packages/sj-i/php-cast)[![Github Actions](https://github.com/sj-i/php-cast/workflows/build/badge.svg)](https://github.com/sj-i/php-cast/actions)[![Coverage Status](https://camo.githubusercontent.com/35afcf09b9fe6432d33afbc3baa5ef1d663f9f0aae8544dca0ea1b7487cc40ec/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f736a2d692f7068702d636173742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sj-i/php-cast?branch=master)[![Psalm coverage](https://camo.githubusercontent.com/1fbebb0c8dcd900d2d3325e351100442b92bfc49d847061965a810efb35e77ad/68747470733a2f2f73686570686572642e6465762f6769746875622f736a2d692f7068702d636173742f636f7665726167652e7376673f)](https://shepherd.dev/github/sj-i/php-cast)

ABOUT
-----

[](#about)

- You can cast any values in the weak mode manner, even if you are in the strict mode (strict\_types=1).

INSTALL
-------

[](#install)

```
composer require sj-i/php-cast
```

SUPPORTED VERSIONS
------------------

[](#supported-versions)

- PHP 7.0+

USAGE
-----

[](#usage)

```
use PhpCast\Cast;
use PhpCast\NullableCast;

// int(1)
$int_value = Cast::toInt('1');
// string(1) "1"
$string_value = Cast::toString(1);
// float(1)
$float_value = Cast::toFloat(1);
// bool(true)
$bool_value = Cast::toBool(1);

// TypeError
$int_value = Cast::toInt('a');
// TypeError
$int_value = Cast::toInt(null);

// int(1)
$int_value = NullableCast::toInt('1');
// string(1) "1"
$string_value = NullableCast::toString(1);
// float(1)
$float_value = NullableCast::toFloat(1);
// bool(true)
$bool_value = NullableCast::toBool(1);

// TypeError
$null_value = NullableCast::toInt('');
// null
$null_value = NullableCast::toInt(null);
```

HOW IT WORKS
------------

[](#how-it-works)

- Return type declarations in weak mode files do the job.
- `PhpCast\Cast` is defined in a file declared as `strict_types=0`
    - Though `strict_types=0` is the current default of PHP, it is explicitly declared to assert the intent.
- The type checks for parameters are done in the caller mode, but the type checks for return values are always done in the callee mode.

"WHY WEAK MODE? I WANT TO USE STRICT MODE IN EVERYWHERE!"
---------------------------------------------------------

[](#why-weak-mode-i-want-to-use-strict-mode-in-everywhere)

- To use `strict_types=1` without any `strict_types=0` codes, explicit casts have to be used for untyped data from external sources like DB or HTTP requests.
- But in PHP, explicit cast like `(int)$foo` never fails with a few exceptions.
    - `(int)'abc'` silently results to `0` and doesn't emit a warning.
- So some validations are necessary before just using casts.
- If explicit casts are used without proper validations by a lazy programmer, the purpose of the type declarations are totally ruined.
    - This was, if I understand it correctly, one of the reasons that strict type checks weren't totally welcomed with open arms during the discussion introducing the scalar type hinting to PHP.
- Defining proper rules of validations and type conversions is sometimes overkill.
    - Even though defining them are always "correct" things to do, in humans life, there are times you don't need to be so correct.
- Let's cast untyped values in the weak mode manner, use the "official" rules of validations and type conversions in the PHP world!
    - No need to invent and learn new rules every day every night everywhere.
    - If the "official" rules doesn't fit your needs, then define and use your own rules selectively there.
- If you've ever read [the accepted RFC of STH](https://wiki.php.net/rfc/scalar_type_hints_v5), you would notice the use of weak mode like this library has been already mentioned [there](https://wiki.php.net/rfc/scalar_type_hints_v5#this_proposal_is_a_compromise). > This proposal is not a compromise. It is an attempt of allowing strict typing to work in PHP. A mechanism to bridge untyped PHP code with strict typed PHP code, a “weak” bridge, would be required (otherwise explicit (type) casts would be needed). This proposal unifies the strict and weak typing into a single system that integrates tightly and behaves consistently.

LICENSE
-------

[](#license)

MIT

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 95.5% 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 ~14 days

Total

4

Last Release

2101d ago

Major Versions

v0.0.3 → v1.0.02020-08-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6488121?v=4)[sji](/maintainers/sj-i)[@sj-i](https://github.com/sj-i)

---

Top Contributors

[![sj-i](https://avatars.githubusercontent.com/u/6488121?v=4)](https://github.com/sj-i "sj-i (21 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

cast

###  Code Quality

TestsPHPUnit

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/sj-i-php-cast/health.svg)](https://phpackages.com/packages/sj-i-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)[zakirullin/mess

Convenient array-related routine &amp; better type casting

21228.9k2](/packages/zakirullin-mess)[wplake/typed

Lightweight PHP utility for seamless type-casting and data retrieval from dynamic variables, arrays, and objects.

231.0k](/packages/wplake-typed)

PHPackages © 2026

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