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

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

mcustiel/typed-php
==================

Object wrappers for primitives and typed arrays in PHP

v0.8.0(9y ago)619PHPPHP &gt;=5.5

Since Feb 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/mcustiel/TypedPhp)[ Packagist](https://packagist.org/packages/mcustiel/typed-php)[ RSS](/packages/mcustiel-typed-php/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

TypedPhp
========

[](#typedphp)

Immutable Object representation for PHP primitive types and Typed Arrays in PHP.

[![Build Status](https://camo.githubusercontent.com/2fd180d918049dffc1634adf316961676e8a93b1ed69e91e0555d321556f279c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f54797065645068702f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/TypedPhp/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/319ed4174f7a317af533364466ded90262ae83496b7fd0f35a2de56bb468cdd2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f54797065645068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/TypedPhp/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/f52bc937ce34761e1c2309a57acf362ab0bf54a74e6c2187cd1b68c418600142/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d6375737469656c2f54797065645068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mcustiel/TypedPhp/?branch=master)

Motivations
-----------

[](#motivations)

In PHP versions 5.5 and 5.6 type hinting is supported for classes and arrays, but not for scalar types. In PHP version 7, type hinting for scalar types is supported, but not typed arrays. So I created object wrappers for scalar types that can be used in PHP 5 and also typed arrays that can be used in PHP7. These objects can be extended to create your own ValueObjects suitable for your domain.

Installation
------------

[](#installation)

### Composer

[](#composer)

```
"require": {
    "mcustiel/typed-php": "*"
}
```

How to use
----------

[](#how-to-use)

All wrappers implement `Primitive` interface:

```
interface Primitive
{
    /**
     * @return mixed
     */
    public function value();
}
```

Also all of them return themselves as string, through implementation of \_\_toString magic method. So you can echo them directly.

### Primitives

[](#primitives)

- DoubleValue
- IntegerValue
- StringValue
- BooleanValue

### Arrays:

[](#arrays)

There are two base types:

- PrimitivesArray
- ObjectsArray

#### PrimitivesArray

[](#primitivesarray)

PrimitivesArray object allows a collection of variables an internal PHP type. And there are already three classes using it:

- DoubleArray
- IntegerArray
- StringArray
- BooleanArray

All of them are constructed with an array as argument. If there is a value in the array of a type that is not correct, an exception will be thrown.

#### ObjectsArray

[](#objectsarray)

This type allows a collection of instances of a given class. An example to create a type for this is as follow:

```
use Mcustiel\TypedPhp\ValueObjects\Multiple\ObjectsArray;

class Foo
{
    private function bar()
    {
        echo 'I am Bar';
    }
}

class FooArray extends ObjectsArray
{
    public function __construct(array $array)
    {
        parent::__construct(Foo::class, $array);
    }
}
```

That's it, you have an array type that accepts only Foo objects. In your code you can use any FooArray object as a regular array.

If there are classes that extend Foo, FooArray will allow them, so polymorphism is supported.

#### Immutable arrays

[](#immutable-arrays)

For each array type provider there is an immutable version, that allows to be created with a set of values and is not allowed to change.

- ImmutableDoubleArray
- ImmutableIntegerArray
- ImmutableStringArray
- ImmutableBooleanArray

Flyweight implementation
------------------------

[](#flyweight-implementation)

A basic flyweight pattern implementation is provided. It will return the exact same value object on multiple calls:

```
use Mcustiel\TypedPhp\Values\IntegerCreator;

$a = IntegerCreator::instance()->getValueObject(5);
$b = IntegerCreator::instance()->getValueObject(5);
```

In this example `$a` and `$b` reference the exact same instance of IntegerValue.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

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

Total

5

Last Release

3390d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8d9b3ff93206038debfa1f16a11bbfc10fca9b2f4ddfdafa00e27365d290cf0d?d=identicon)[mcustiel](/maintainers/mcustiel)

---

Top Contributors

[![mcustiel](https://avatars.githubusercontent.com/u/3268370?v=4)](https://github.com/mcustiel "mcustiel (53 commits)")

---

Tags

phpprimitive-typestype-hintstyped-arraysvalue-objectphpvalue objectstypedprimitives

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21422.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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