PHPackages                             skyzyx/strong-types - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. skyzyx/strong-types

Abandoned → Upgrade your PHP.ArchivedLibrary[Validation &amp; Sanitization](/categories/validation)

skyzyx/strong-types
===================

Enables strong types for PHP. This allows for tighter validation, especially when accepting input from users.

1.0.9(11y ago)1156[15 issues](https://github.com/skyzyx/php-strong-types/issues)MITPHPPHP &gt;=5.4.0

Since Nov 18Pushed 9y ago2 watchersCompare

[ Source](https://github.com/skyzyx/php-strong-types)[ Packagist](https://packagist.org/packages/skyzyx/strong-types)[ RSS](/packages/skyzyx-strong-types/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (11)Versions (11)Used By (0)

PHP Strong Types
================

[](#php-strong-types)

[![Source](https://camo.githubusercontent.com/3fe593a9e5c75b31660757a9b38e69cecc8dcfcc40b74fe561cf0f319f0701c0/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d736b797a79782f7068702545322538302539337374726f6e6725453225383025393374797065732d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/skyzyx/php-strong%E2%80%93types)[![Latest Stable Version](https://camo.githubusercontent.com/9af43952ce9c3bb0903e8612c85daad6603fc4df71798c8b1ec8be56b394adcc/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736b797a79782f7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/skyzyx/strong-types)[![Total Downloads](https://camo.githubusercontent.com/5faa582d9b91f3b4d8f6404db13e24e36c5757c51aef1936ce5ec229ffe2bc84/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736b797a79782f7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/skyzyx/strong-types)[![Open Issues](https://camo.githubusercontent.com/da4682a0cf450dfce52a6f81c2b94db1a1e5d996c8eac69d43bed318b10214cf/687474703a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f736b797a79782f7068702d7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://github.com/skyzyx/php-strong-types/issues)[![Build Status](https://camo.githubusercontent.com/fd9f24b613a24f273cf270df6f50b5a3b7f2369ac3e14f2c9bf0d5a9b17e9cca/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f736b797a79782f7068702d7374726f6e672d74797065732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/skyzyx/php-strong-types)[![Coverage Status](https://camo.githubusercontent.com/358b5da9ce5c5bc10e27cac2d628975d722cd680d445cf90127e7bef7df74c48/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f736b797a79782f7374726f6e672d74797065732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/skyzyx/php-strong-types?branch=master)[![Code Climate](https://camo.githubusercontent.com/bbc4f328403efc7856bc01b85f9eb1e87a80a3fc64f837c44511c2441809c873/687474703a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6769746875622f736b797a79782f7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/skyzyx/php-strong-types)[![Code Quality](https://camo.githubusercontent.com/443e11ef8c6979fbf47511cfd883e9096c774e776d907969db2671508f9326a7/687474703a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f736b797a79782f7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/skyzyx/php-strong-types/)[![Dependency Status](https://camo.githubusercontent.com/df7d374a57f3dad5e9e6bfd33341c17ac211207d1a230169f26b015e6d74e5da/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3534366230303639393530383235313933643030303065632f62616467652e7376673f7374796c653d666c61742d737175617265)](https://www.versioneye.com/user/projects/546b0069950825193d0000ec)[![SensioLabsInsight](https://camo.githubusercontent.com/e3f9774712fd8fe6e318dc1e5e503d02e1fec2780213fbb1516338393a35c568/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f39373562303630362d613632342d346531662d623363302d3338356131663036653636662f6d696e692e706e67)](https://insight.sensiolabs.com/projects/975b0606-a624-4e1f-b3c0-385a1f06e66f)[![HHVM Support](https://camo.githubusercontent.com/7eadffa54875392cc7567dfadb8f5c669f89a9b32cf4535bd8521acb80fd585a/687474703a2f2f696d672e736869656c64732e696f2f6868766d2f736b797a79782f7374726f6e672d74797065732e7376673f7374796c653d666c61742d737175617265)](https://hhvm.com)[![Documentation Status](https://camo.githubusercontent.com/e905df2728788f34e6ca8722ccfb9f3ffcd6851b7438169a280e6b6794768f63/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f736b797a79782d7374726f6e672d74797065732f62616467652f3f76657273696f6e3d6d6173746572267374796c653d666c61742d737175617265)](https://readthedocs.org/projects/shared-utilities/?badge=master)[![License](https://camo.githubusercontent.com/5d046a45ea8bfec22651a5b83b8694a284e5f8e0402240a0d0955ada3fe3798d/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736b797a79782f7374726f6e672d74797065732d626c75652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/skyzyx/strong-types)[![Author](https://camo.githubusercontent.com/efe0ace262a01c191d3a926f4cda2e7dad108d0f36d1cbe0f6dab11185ff9bc4/687474703a2f2f696d672e736869656c64732e696f2f62616467652f617574686f722d40736b797a79782d626c75652e7376673f7374796c653d666c61742d737175617265)](https://twitter.com/skyzyx)

Enables strong types for PHP. This allows for tighter validation, especially when accepting input from users.

**Why would anyone do this?** This can be useful when developing APIs, and you want to require strict types. By defining those types/shapes ahead of time as classes, you can enforce incoming/outgoing data types, but also access the native values after the vaidation step has occurred.

It intentionally rejects and avoid any kind of "type massaging". If you pass an `integer` to `StringType`, you will get an exception. All errors are thrown as Exceptions with useful error messages.

Features
--------

[](#features)

- BooleanType
- Collection (ArrayAccess)
- Enum
- FloatType
- IntegerType
- StringType (incl. Utf8String)

`DateTime` is already strongly typed, so use that class for strong date/time types.

Examples
--------

[](#examples)

We can do simple validation enforcement, which can be valuable in PHP 5.x. (Use strict types instead in PHP 7.)

```
use Skyzyx\StrongTypes\StringType;

$abc = new StringType('abc');

$v123 = new StringType(123);
#=> UnexpectedValueException:
#=> The Skyzyx\StrongTypes\StringType class expects a value of type string.
#=> Received a value of type integer instead.

$abc->getValue();
#=> 'abc'
```

You can also extend the base types to create more specific *data types* (aka, *data shapes*).

```
use Skyzyx\StrongTypes\StringType;

class FiveChars extends StringType
{
    public function __construct($s)
    {
        $this->setExactLength(5);
        parent::__construct($s);
    }
}

$abcde = new FiveChars('abcde');

$abc = new FiveChars('abc');
#=> Exception

$v12345 = new FiveChars(12345);
#=> Exception
```

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

[](#installation)

Using [Composer](https://getcomposer.org):

```
composer require skyzyx/strong-types=~2.0
```

And include it in your scripts:

```
require_once 'vendor/autoload.php';
```

Testing
-------

[](#testing)

Firstly, run `composer install -o` to download and install the dependencies.

You can run the tests as follows:

```
bin/phpunit
```

Contributing
------------

[](#contributing)

Here's the process for contributing:

1. Fork PHP Strong Types to your GitHub account.
2. Clone your GitHub copy of the repository into your local workspace.
3. Write code, fix bugs, and add tests with 100% code coverage.
4. Commit your changes to your local workspace and push them up to your GitHub copy.
5. You submit a GitHub pull request with a description of what the change is.
6. The contribution is reviewed. Maybe there will be some banter back-and-forth in the comments.
7. If all goes well, your pull request will be accepted and your changes are merged in.

Authors, Copyright &amp; Licensing
----------------------------------

[](#authors-copyright--licensing)

- Copyright (c) 2014–2016 [Ryan Parman](http://ryanparman.com).

See also the list of [contributors](/skyzyx/strong-types/contributors) who participated in this project.

Licensed for use under the terms of the [MIT](http://www.opensource.org/licenses/mit-license.php) license.

Coding Standards
----------------

[](#coding-standards)

PSR-0/1/2 are a solid foundation, but are not an entire coding style by themselves. I have taken the time to document all of the nitpicky patterns and nuances of my personal coding style. It goes well-beyond brace placement and tabs vs. spaces to cover topics such as docblock annotations, ternary operations and which variation of English to use. It aims for thoroughness and pedanticism over hoping that we can all get along.

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 96.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 ~11 days

Recently: every ~24 days

Total

10

Last Release

4141d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/39447?v=4)[Ryan Parman](/maintainers/skyzyx)[@skyzyx](https://github.com/skyzyx)

---

Top Contributors

[![skyzyx](https://avatars.githubusercontent.com/u/39447?v=4)](https://github.com/skyzyx "skyzyx (55 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (2 commits)")

---

Tags

typehhvmstrong

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/skyzyx-strong-types/health.svg)

```
[![Health](https://phpackages.com/badges/skyzyx-strong-types/health.svg)](https://phpackages.com/packages/skyzyx-strong-types)
```

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

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