PHPackages                             chippyash/strong-type - 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. chippyash/strong-type

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

chippyash/strong-type
=====================

Strong type support. For when you absolutely want to know what you are getting

5.1.0(7y ago)616.6k311BSD-3-ClausePHPPHP &gt;=5.6CI failing

Since Aug 5Pushed 7y ago1 watchersCompare

[ Source](https://github.com/chippyash/Strong-Type)[ Packagist](https://packagist.org/packages/chippyash/strong-type)[ Docs](http://zf4.biz/packages?utm_source=packagist&utm_medium=web&utm_campaign=blinks&utm_content=strongtype)[ RSS](/packages/chippyash-strong-type/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (1)Versions (22)Used By (11)

chippyash/Type
==============

[](#chippyashtype)

Quality Assurance
-----------------

[](#quality-assurance)

[![PHP 5.6](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)](https://camo.githubusercontent.com/88093c79af42bd3c07f4d6aa378289e1f5450411c56753b0323bd7d8b9b1f9ee/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d352e362d626c75652e737667)[![PHP 7](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)](https://camo.githubusercontent.com/d23ce60b89c28c023d0ca69981ec9afbb17eb08a9cd1b609fd84c15d0732b7ce/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372d626c75652e737667)[![Build Status](https://camo.githubusercontent.com/6a63a7af574f24bee733c7049630e61a5c8f15323b09fa73c01af194d1f103a0/68747470733a2f2f7472617669732d63692e6f72672f6368697070796173682f5374726f6e672d547970652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/chippyash/Strong-Type)[![Test Coverage](https://camo.githubusercontent.com/6cf275f13c7dcb436450611cf86da697befe07dc5a04e3036a14e36c9dd30c07/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f5374726f6e672d547970652f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/chippyash/Strong-Type/coverage)[![Code Climate](https://camo.githubusercontent.com/0f4aa24b968f381d0acbba514271b1de8f30b24f19823ca698620f8518f2a47a/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6368697070796173682f5374726f6e672d547970652f6261646765732f6770612e737667)](https://codeclimate.com/github/chippyash/Strong-Type)

The above badges represent the current development branch. As a rule, I don't push to GitHub unless tests, coverage and usability are acceptable. This may not be true for short periods of time; on holiday, need code for some other downstream project etc. If you need stable code, use a tagged version. Read 'Further Documentation' and 'Installation'.

Please note that developer support for PHP5.3 was withdrawn at version 4.0.0 of this library. It may be that the code will continue to run for you at later versions, but you must ascertain that for yourself. If you need support for PHP 5.3, please use a version `>=3,=4,set(RequiredType::TYPE_NATIVE);
```

at the start of your code. This will in turn call the setNumberType methods on the other factories, so you don't need to do that

If you want to get the gmp typed value of a number you can call its gmp() method.

```
    //assuming we are running under gmp
    $i = TypeFactory::create('int', 2); //returns GMPIntType
    $i = TypeFactory::create('whole', -1); //returns GMPIntType
    $i = TypeFactory::create('natural', 0); //returns GMPIntType
    $gmp = $i->gmp(); //returns resource or GMP object depending on PHP version

    $r = TypeFactory::create('rational', 2, 3); //returns GMPRationalType
    $r = TypeFactory::create('float', 2/3); //returns GMPRationalType
    $gmp = $i->gmp(); //returns resource or GMP object depending on PHP version

    $r = TypeFactory::create('rational', 2, 3); //returns GMPRationalType
    $gmp = $r->gmp(); //returns array of gmp types, [numerator, denominator]

    $c = TypeFactory::create('complex', '2+3i'); //returns GMPComplexType
    $gmp = $c->gmp(); //returns array of gmp types, [[num,den],[num,den]] i.e. [r,i]
```

All GMP types support the GMPInterface, so in addition to having the gmp() method, they will also have:

- public function asGMPIntType() Return number as GMPIntType number. Will return floor(n/d) for rational types
- public function asGMPComplex() : Return the number as a GMPComplex number i.e. n+0i
- public function asGMPRational(): Return number as GMPRational number.

Trying to keep track of what types you are actually instantiating is made much easier if you use the type factories, as they know which types to create. **Therefore if you want your code to be runnable as PHP native or GMP, use the factories to create your numeric types.**

Further documentation
---------------------

[](#further-documentation)

You can find the [API documentation here](http://chippyash.github.io/Strong-Type)

[Test Contract](https://github.com/chippyash/Strong-Type/blob/master/docs/Test-Contract.md) in the docs directory.

Check out [ZF4 Packages](http://zf4.biz/packages?utm_source=github&utm_medium=web&utm_campaign=blinks&utm_content=strongtype) for more packages

### UML

[](#uml)

[![class diagram](https://github.com/chippyash/Strong-Type/raw/master/docs/strong-type-class.png)](https://github.com/chippyash/Strong-Type/blob/master/docs/strong-type-class.png)

Changing the library
--------------------

[](#changing-the-library)

1. fork it
2. write the test
3. amend it
4. do a pull request

Found a bug you can't figure out?

1. fork it
2. write the test
3. do a pull request

NB. Make sure you rebase to HEAD before your pull request

Or - raise an issue ticket.

Where?
------

[](#where)

The library is hosted at [Github](https://github.com/chippyash/Strong-type). It is available at [Packagist.org](https://packagist.org/packages/chippyash/strong-type)

### Installation

[](#installation)

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

#### For production

[](#for-production)

Use V5 unless you have a strong reason not to.

```
    "chippyash/strong-type": ">=5.0.0,
