Money
A PHP implementation of Fowler's Money pattern for safe currency math, formatting, and conversion.
Repository Health
Technical Analysis
Money (moneyphp/money) is a widely used PHP library that implements Martin Fowler’s Money pattern as an immutable value object. It stops the classic mistake of representing monetary values with floats by storing amounts as integer minor units (strings internally for unlimited precision) paired with a Currency, so arithmetic is exact and currency-aware.
The library provides safe add, subtract, multiply, and allocate operations, comparison methods, ISO and custom currency lists, multiple formatters (including intl-based localization and Bitcoin), and a converter with pluggable exchange-rate sources. It is a foundational dependency across PHP e-commerce, accounting, and fintech applications.
What You Get
- An immutable Money value object storing amounts as integer minor units
- Exact arithmetic: add, subtract, multiply, divide, and allocate
- ISO 4217 currency data plus crypto and custom currency lists
- Formatters including intl localization, decimal, and Bitcoin
- A Converter with pluggable exchange-rate implementations
Common Use Cases
- Representing prices, totals, and balances without float errors
- Splitting an amount across recipients with fair remainder allocation
- Formatting monetary values localized to a user’s locale
- Converting between currencies using live or fixed exchange rates
Under The Hood
Architecture - The core is an immutable Money value object composed with a Currency, backed by pluggable Calculator implementations (BcMathCalculator, GmpCalculator) selected at runtime for arbitrary-precision arithmetic on string amounts. Surrounding it are Currencies providers (ISO, crypto, custom, aggregate, cached), Formatter and Parser hierarchies, and an Exchange/Converter layer for currency conversion. Interfaces for each concern keep the object graph extensible.
Tech Stack - Targets PHP 8.1 through 8.5 and requires ext-bcmath and ext-filter, with ext-gmp and ext-intl as optional accelerators/formatters. It integrates with moneyphp/iso-currencies and moneyphp/crypto-currencies for data and florianv/swap for exchange rates. Development uses PHPUnit, PHPBench, PHPStan, Psalm, and the Doctrine coding standard.
Code Quality - This is a mature, rigorously engineered codebase: extensive unit tests, benchmarks, static-analysis fixtures (static-analysis/, hack/), and strict CI. Amounts moved to string storage in v3 to support unlimited integers, reflecting careful attention to numeric correctness. The project is actively maintained with a broad contributor base.
API Design - The public API is expressive and safe: named constructors like Money::EUR(500), fluent immutable operations (add, subtract, multiply, allocate, compareTo), and clear exceptions when mixing currencies. Formatters and parsers are injected explicitly, so the common path is simple while advanced formatting and conversion remain fully configurable.