PHPackages                             theodorejb/polycast - 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. theodorejb/polycast

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

theodorejb/polycast
===================

Safely cast values to int, float, or string

v1.0.0(10y ago)50261.1k↓35.5%21MITPHPCI failing

Since Oct 7Pushed 4y ago4 watchersCompare

[ Source](https://github.com/theodorejb/PolyCast)[ Packagist](https://packagist.org/packages/theodorejb/polycast)[ RSS](/packages/theodorejb-polycast/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (12)Used By (1)

PolyCast
========

[](#polycast)

Provides `safe_int`, `safe_float`, and `safe_string` functions. The functions return true if a value can be cast to the designated type without data loss, and false if it cannot.

Three complementary functions are also included: `to_int`, `to_float`, and `to_string`. These functions cast and return a value if the corresponding *safe\_* function returns true, and throw a `CastException` if it returns false.

This library was originally based on the [Safe Casting Functions RFC](https://wiki.php.net/rfc/safe_cast)proposed (but ultimately declined) for PHP 7. For additional background info see [PolyCast: a library for safe type conversion in PHP](https://theodorejb.me/2015/10/25/polycast/).

Acceptable casts
----------------

[](#acceptable-casts)

### `safe_int`

[](#safe_int)

- Integers
- Floats without a remainder between `PHP_INT_MIN` and `PHP_INT_MAX`
- Strings with an optional positive/negative sign, without leading zeros, and containing the digits 0-9 with a value between `PHP_INT_MIN` and `PHP_INT_MAX`.

### `safe_float`

[](#safe_float)

- Floats
- Integers
- Strings with an optional positive/negative sign matching the format described at .

### `safe_string`

[](#safe_string)

- Strings
- Integers
- Floats
- Objects with a `__toString` method

The *safe\_* functions will always return false if passed `null`, `true` or `false`, an array, resource, or object (with the exception of objects with a `__toString` method passed to `safe_string`).

Install via Composer
--------------------

[](#install-via-composer)

`composer require theodorejb/polycast`

Usage examples
--------------

[](#usage-examples)

### Input validation

[](#input-validation)

```
use function theodorejb\polycast\{ safe_int, safe_float, safe_string };

if (!safe_string($_POST['name'])) {
    echo 'Name must be a string';
} elseif (!safe_int($_POST['quantity'])) {
    echo 'Quantity must be an integer';
} elseif (!safe_float($_POST['price'])) {
    echo 'Price must be a number';
} else {
    addProduct($_POST['name'], (int)$_POST['quantity'], (float)$_POST['price']);
}

function addProduct(string $name, int $quantity, float $price)
{
    // ... a database query would go here
}
```

### Safe type conversion

[](#safe-type-conversion)

```
use theodorejb\polycast;

try {
    $totalRevenue = 0.0;
    $totalTransactions = 0;

    foreach ($csvRows as $row) {
        $totalRevenue += polycast\to_float($row['monthly_revenue']);
        $totalTransactions += polycast\to_int($row['monthly_transactions']);
    }

    // do something with totals
} catch (polycast\CastException $e) {
    echo "Error: " . $e->getMessage();
    var_dump($e->getTrace());
}
```

Author
------

[](#author)

Theodore Brown

License
-------

[](#license)

MIT

###  Health Score

41

—

FairBetter than 87% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity70

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

Recently: every ~84 days

Total

11

Last Release

3904d ago

Major Versions

v0.9.0 → v1.0.02015-10-25

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3053271?v=4)[Theodore Brown](/maintainers/theodorejb)[@theodorejb](https://github.com/theodorejb)

---

Top Contributors

[![theodorejb](https://avatars.githubusercontent.com/u/3053271?v=4)](https://github.com/theodorejb "theodorejb (50 commits)")

---

Tags

phptype-safety

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/theodorejb-polycast/health.svg)

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

###  Alternatives

[offload-project/laravel-waitlist

Manage multiple waitlists in Laravel - perfect for beta programs, product launches, and feature access control

532.0k](/packages/offload-project-laravel-waitlist)

PHPackages © 2026

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