PHPackages                             dreamscapes/scalars - 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. dreamscapes/scalars

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

dreamscapes/scalars
===================

Scalar types in PHP defined as objects to allow more type safety

06PHP

Since May 6Pushed 11y ago1 watchersCompare

[ Source](https://github.com/Dreamscapes/scalars)[ Packagist](https://packagist.org/packages/dreamscapes/scalars)[ RSS](/packages/dreamscapes-scalars/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Scalars
=======

[](#scalars)

[![Built with GNU Make](https://camo.githubusercontent.com/733e0772a8e294ac74209acd977d00add7736f4a6b1dc872da2b3f374d465c9f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c74253230776974682d474e552532304d616b652d627269676874677265656e2e737667)](https://camo.githubusercontent.com/733e0772a8e294ac74209acd977d00add7736f4a6b1dc872da2b3f374d465c9f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c74253230776974682d474e552532304d616b652d627269676874677265656e2e737667)

> Scalar value typehinting in PHP

About
-----

[](#about)

With this library, you can use function/method typehinting for scalar values just like you would use it with objects.

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

[](#installation)

Use Composer:

`composer require dreamscapes/scalars:dev-master` (visit [Packagist](https://packagist.org/packages/Dreamscapes/scalars) for list of all available versions)

Usage
-----

[](#usage)

The following scalar types are available as classes:

- **Scalar** - accepts any scalar value (bool, int, float, string)
- **Number** - accepts any numeric value (float, int)
- **Int** - accepts only integers
- **Float** - accepts only floating point numbers
- **Bool** - accepts only booleans
- **String** - accepts only strings

Any of these can be used in function / method signatures:

```
function takeString(String $str) { /* do stuff with $str */ }
```

To pass a string into such function, you have two, completely equivalent options:

```
takeString(String('my string'));
// or...
takeString(new String('my string'));
```

And, within the function itself, you can get to the actual value passed by doing one of the following:

```
function takeString(String $str)
{
    // $str is instance of String, but we need the actual string
    // to do something useful with it:
    $str = $str->val;
    // or...
    $str = $str();
}
```

### What if I use incorrect scalar type?

[](#what-if-i-use-incorrect-scalar-type)

Having type hints without type safety would be like having classes without instances. And so, if you attempt to do something like this

```
// WARNING - BAD!
$bool = Bool('but I am a string!');
```

You will trigger an error (`E_USER_ERROR` to be precise):

> *Invalid type supplied for Bool, string given*

**Note:** Do not attempt to convert these to exceptions and catch them - these kinds of errors are programmers' errors and should be fixed immediately.

### Casting into different types

[](#casting-into-different-types)

If you have a value and you want it to be treated as a particular scalar type (i.e. you have an integer and want it to be treated as boolean), you can use one of the following, completely equivalent options:

```
$bool = Bool((bool)'I will be truthy!');
// or...
$bool = Bool::cast('I will also be truthy!');
```

The `Scalar::cast()` method utilises PHP's internal scalar conversion mechanism using [`settype`](http://php.net/manual/en/function.settype.php). Whether that is a good or a bad thing is left to your own discretion.

### Where are arrays?

[](#where-are-arrays)

Arrays are not a scalar type. And you can typehint arrays without any syntactic sugar.

License
-------

[](#license)

This software is licensed under the **BSD (3-Clause) License**. See the [LICENSE](LICENSE) file for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/4300d8bf145f9d0db6e15c90199842aad7541376402c2fc2ee86539631100bc4?d=identicon)[Alaneor](/maintainers/Alaneor)

---

Top Contributors

[![robertrossmann](https://avatars.githubusercontent.com/u/3058150?v=4)](https://github.com/robertrossmann "robertrossmann (1 commits)")

### Embed Badge

![Health badge](/badges/dreamscapes-scalars/health.svg)

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

PHPackages © 2026

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