Download the PHP package khaledalam/unit without Composer
On this page you can find all versions of the php package khaledalam/unit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download khaledalam/unit
More information about khaledalam/unit
Files in khaledalam/unit
Package unit
Short Description A lightweight, type-safe PHP library for units, quantities, and dimensional analysis with automatic conversion.
License MIT
Homepage https://github.com/khaledalam/unit
Informations about the package unit
Unit
Type-safe units, quantities & dimensional analysis for PHP
A lightweight, immutable, type-safe PHP library for working with quantities, units, and dimensional analysis. Define units, convert across compatible ones, and do arithmetic that refuses to add meters to seconds.
No setup, no registration — common units are ready the moment you install.
▶️ Try the live demo — an interactive converter running in your browser.
Why Unit?
| Unit | Hand-rolled * factor |
Raw floats | |
|---|---|---|---|
Blocks nonsensical math (m + s) |
✅ | ❌ | ❌ |
| Auto-converts compatible units | ✅ | ❌ | ❌ |
| Affine temperature scales (°C/°F/K) | ✅ | ⚠️ easy to get wrong | ❌ |
Derived units (m/s, m²) |
✅ | ❌ | ❌ |
| Immutable value objects | ✅ | — | ❌ |
| Zero dependencies | ✅ | ✅ | ✅ |
If you've ever shipped a bug because someone stored centimeters in a "meters" column, this library is for you.
Installation
Requires PHP 8.2+.
Quick start
Features
- ✅ Immutable value objects
- ✅ Dimensionally-aware arithmetic (
add,subtract,multiply,divide) - ✅ Automatic conversion between compatible units (e.g. cm → m)
- ✅ Correct affine temperature conversion (°C ↔ °F ↔ K)
- ✅ Derived/compound units rendered as
m/s,m²,m·s - ✅ Scalar math (
times,dividedBy,pow,sqrt,abs,negate) - ✅ Named dimensions (
dimensionName()→"velocity") - ✅ Comparisons (
equals,isGreaterThan,isLessThan) - ✅
JsonSerializableoutput, Laravel cast & Doctrine type - ✅ Enum-powered unit naming and a custom unit registry
- ✅ Zero runtime dependencies
Usage
Conversion
Arithmetic
All operations return new Quantity objects with the proper dimension and unit.
Scalar math
Dimension names
Temperature (affine scales)
Parsing strings
Humanize (auto-pick the best unit)
Comparison
Precision & formatting
JSON
Custom units
Register your own units against a dimension:
Incompatible operations throw
Supported units
| Dimension | Units |
|---|---|
| Length | mm, cm, m, km, in, ft, yd, mi |
| Mass | mg, g, kg, t, lb, oz |
| Time | ms, s, min, h, d, wk |
| Area | cm², m², km², ft², ha, acre |
| Volume | mL, L, m³, gal |
| Temperature | °C, °F, K |
| Speed | m/s, km/h, mph, kn |
| Data | bit, B, KB, MB, GB, TB, KiB, MiB, GiB, TiB |
| Force | N, kN |
| Energy | J, kJ, cal, kcal, Wh, kWh |
| Power | W, kW, hp |
| Pressure | Pa, kPa, bar, atm, psi |
| Frequency | Hz, kHz, MHz, GHz |
| Angle | rad, deg, grad, turn |
Need more? Open an issue or register your own.
Laravel integration
The package ships a service provider (auto-discovered) and an Eloquent cast. Store a
quantity in a single column and get a Quantity back on access:
Register app-specific units via config/unit.php:
Requires
illuminate/database(already present in any Laravel app).
Symfony / Doctrine integration
A Doctrine DBAL type persists a Quantity as a string column. Register it once, then
map any column with it:
Strings assigned to the property are parsed on write, and the column hydrates back into a
Quantity on read.
Requires
doctrine/dbal^4.
Examples
Runnable scripts live in examples/:
Testing & quality
The suite combines PHPUnit test classes with .phpt scenario tests and holds 100%
line coverage, verified on PHP 8.2, 8.3, and 8.4 in CI (PHPStan level max, PSR-12).
Contributing
Contributions are welcome! Please read CONTRIBUTING.md and the Code of Conduct. Adding a unit is usually a three-line change.
License
MIT © Khaled Alam