Download the PHP package elegant-bro/money without Composer
On this page you can find all versions of the php package elegant-bro/money. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package money
Elegant money implementation
This library provides classes for money manipulation using a pure object-oriented approach.
Yet another PHP-money, why?
Why did we make yet another PHP-library for money? After all, there are:
But, there are disadvantages to using it in pure object-oriented development.
- There are no interfaces — we can't create an object that will represent money itself.
- All existed implementations are classes with hundreds loc and dozens of methods like
add
,sub
,divide
,compare
etc., which are essentially procedural style. - We can't add new functionality as there are no interfaces, and most implementations are final.
- Eager execution — all calculations happen in construct-time as constructor's arguments should be evaluated.
- Existed libraries hide the scale, the only thing we can control is a rounding method.
Our solution
- We have the
Money
interface! This implementation is not procedural DTO. - There is no hundreds-lines class with tens of methods. Every operation is an object that implements the
Money
interface. - Lazy execution: you can construct complex expressions without immediate calculations.
- Explicit scale to avoid ambiguous results.
The presence of the interface allows you to make different classes. For example, you can easily implement UserBalance
that takes data from a database or some API or even from a file.
Besides, you can create some static implementations like TwoDollars
:
You are free to use those objects in any out-of-the-box or custom operations.
Let's take a look at the following example: you need some tax which is 10%
of the given amount but not less than $2
.
This is a real object-oriented solution:
This is a shiny declarative code, but the biggest advantage is laziness. No one calculation will happen until the amount
method will be called.
Using procedural implementation like moneyphp/money
you have to create some sort of ugly MoneyHelper
with static tax
method:
It is quite possible that this result will not be needed in the subsequent calculation. For example, there is some tax-free condition, and you should check it before the tax calculation to avoid unnecessary DB-request and end up with temporal coupling.
Installation
For contributors
Pass all tests locally before creating the pull request.
Build the test container and run all tests
Other commands