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

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

rfx/cast
========

Cast / convert a stdClass or any other class to another class

v1.1.0(4y ago)168↓88.9%LGPL-2.1-onlyPHP

Since May 16Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (3)Used By (0)

Cast / convert a stdClass (or any other class) to another class
===============================================================

[](#cast--convert-a-stdclass-or-any-other-class-to-another-class)

[![Minimum PHP version: 7.4.0](https://camo.githubusercontent.com/89e54687773afb25f5943cfbd66376d73e2ce50874c54429a78aac45cb6bcf84/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e342e302532422d626c75652e737667)](https://camo.githubusercontent.com/89e54687773afb25f5943cfbd66376d73e2ce50874c54429a78aac45cb6bcf84/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e342e302532422d626c75652e737667)

ABOUT
-----

[](#about)

Allows you to cast a stdClass (or any other class) to some other class.

INSTALL
-------

[](#install)

```
composer require rfx/cast
```

USAGE
-----

[](#usage)

### Problem

[](#problem)

So you received a beautiful object that you want to use

```
stdClass Object
(
    [name] => home
    [at] => stdClass Object
        (
            [x] => 4
            [y] => 5
        )
)

```

However:

- Your IDE hates it
- All static analysis tools hate it
- No autocompletion
- No type checks

### Solution

[](#solution)

```
declare(strict_types=1);
// Create class(es) that define(s) those properties
use rfx\Type\Cast;
class Point {
    public int $x;
    public int $y;
}

class Location {
    public string $name;
    public Point $at;
}

function getLocation(): Location {
    // Get your object (from a json source, some external lib, ...).
    // As demonstration we create one here from an array.
    $obj = (object)['name' => 'home', 'at' => ['x' => 4, 'y' => 5]];
    // Cast it (this works recursively, e.g. Location contains a Point object)
    return Cast::as($obj, Location::class);
}

$l = getLocation();
```

### Result

[](#result)

```
Location Object
(
    [name] => home
    [at] => Point Object
        (
            [x] => 4
            [y] => 5
        )
)

```

- Your IDE loves you again
- All static analysis tools will help you find problems
- Autocompletion, type checks, etc. work again

### Speed

[](#speed)

If performance is important and:

- you are casting many objects of the same type
- all your objects are shallow (no nesting)
- all the properties are scalar types

then you can use the faster alternative, which is as performant as a normal constructor:

```
declare(strict_types=1);
// Create a proper class that defines those properties
use rfx\Type\Cast;
class Point {
    public string $name;
    public int $x;
    public int $y;
}

/** @return Point[] */
function getPoints(): array {
    // Get your objects (from a json source, some external lib, ...).
    // As demonstration we create a million from an array.
    $obj = (object)['name' => 'P1', 'x' => 4, 'y' => 5];
    $objs = array_fill(0, 1000000, $obj);
    // Create a factory
    $cf = new Cast(Point::class);
    // Cast them all
    return array_map([$cf, 'cast'], $objs);
}

$p = getPoints();
```

LIMITATIONS
-----------

[](#limitations)

This section needs to be written, and some future plans added.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

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

Total

2

Last Release

1461d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/04d737013ee5ef6a548174900befefc8d7689a16fbb78687dc6b41386801a26c?d=identicon)[rokfaith](/maintainers/rokfaith)

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[scriptixru/sypexgeo

Supports GeoIP services (sypexgeo.net).

3328.9k](/packages/scriptixru-sypexgeo)[david-r-edgar/rdp

Ramer–Douglas–Peucker algorithm for polyline simplification

2332.7k](/packages/david-r-edgar-rdp)[junohamburg/kirby-visual-block-selector

Kirby Visual Block Selector

593.9k](/packages/junohamburg-kirby-visual-block-selector)[gongo/merciful-polluter

Emulate `register\_globals` and `magic\_quotes\_gpc` in PHP 5.4 or higher

1715.0k](/packages/gongo-merciful-polluter)[avram/vokativ

Library used to convert first names from Serbian language into their vocative case.

1318.4k](/packages/avram-vokativ)

PHPackages © 2026

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