Money
Money
Money class.
This class is used to extend a normal integer value by adding currency related features such as the currency type (AUD, USD, JPY, etc) and realtime currency conversion using Yahoo Quotes.
Example
$aud = new Money(500, 'AUD');
$usd = new Money(200, 'USD');
$total = $aud->add($usd);The default money format is '%.2n' which will format the value to whole dollar with 2 decimal places. ie: $123.45. You can specify the format when retrieving the amount (see self::format()) or you can set the default format at any time.
You can also set the default currency code to use when none is specified.
It is recommended that these be set in your bootstrap file so that they are consistent across the whole application.
Example bootstrap.php
Hazaar\self::$defaultCurrency = 'AUD';Properties
defaultCurrency
public string $defaultCurrencyvalue
private float $valuelocalCurrency
private array $localCurrencydb
public BTree $dbexchangeRates
private array $exchangeRatescache
public Adapter $cacheMethods
__construct
The money class constructors takes two parameters. The value of the currency and the type of
public __construct(float $value, ?string $currency): voidcurrency the value is representative of.
Currency info is loaded from a built-in support file named countryInfo.txt. This file contains country information including country codes, currency names, etc. The first time a currency is used this information is loaded into memory only once and is shared between all currency objects.
A cache object is also set up for use by the exchange conversion methods. It will attempt to use the APC cache backend but if that is not available it will fall back to the file backend.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | float | The currency value amount |
$currency | string | The name of the currency or country of origin. Ie: 'USD' and 'US' will both resolve to US dollars. |
__toString
This magic function is called when PHP tries to automatically convert the currency to a string.
public __toString(): stringSimply calls the self::toString() method.
getCurrencyInfo
Retrieves information about a currency.
public getCurrencyInfo(?string $currency): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$currency | string | The currency code. If null, returns information about all currencies. |
getCurrencyCode
Get either the default currency code, or get a currency code for a country. Use this instead of
public getCurrencyCode(?string $code): stringaccessing self
Parameters
| Parameter | Type | Description |
|---|---|---|
$code | string |
getCode
Get the currency code for the current currency object or look up the currency code for a country.
public getCode(?string $country): stringThis value is normalised during object instantiation. This means that if you specify a country upon instantiation, this will still return the correct currency code.
Optionally, if a country parameter is specified, this method can be used to look up the currency code for that country.
Example
echo $currency->getCode('au'); //This will echo the string 'AUD'.Parameters
| Parameter | Type | Description |
|---|---|---|
$country | string | optional country code to look up a currency code for |
getCurrencySymbol
Get the symbol for the current currency. The currency symbol is usually prefixed to the currency
public getCurrencySymbol(): stringamount. This method doesn't actually return the currency symbol as such, but will return the HTML entity name of the currency symbol, for example 'dollar', 'pound', 'yen', etc.
getExchangeRate
Get the current exchange rate for the currency value against a foreign currency. This method uses
public getExchangeRate(string $foreignCurrency): floatthe Yahoo Quotes service to get the current exchange rate. For this method to work your host needs to have web access (ie: port 80). This should 'just work' for all but a small number of cases.
Because this method contacts another web service the response can be a little slow. Because of this results are cached so that subsequent requests for the same conversion will be faster.
Parameters
| Parameter | Type | Description |
|---|---|---|
$foreignCurrency | string | the foreign currency to get an exchange rate for |
convertTo
Convert the currency object to another currency and return a new Money object.
public convertTo(string $foreignCurrency): MoneyParameters
| Parameter | Type | Description |
|---|---|---|
$foreignCurrency | string | The currency to convert to. Can be country or currency code. |
format
The format method will format the currency value amount to an international standard format of
public format(?string $format): string{symbol}{amount}{code}. For example, US dollars will be expressed as $100USD. Australian dollar as $105AUD and so on.
Parameters
| Parameter | Type | Description |
|---|---|---|
$format | string | An optional format passed to the money_format function. If not specified the global default format will be used. |
toString
Convert currency to a string. Outputs the same as the self::format() method using the default
public toString(): stringformat.
toFloat
Get the currency as a float value formatted using the money_format PHP function with optional
public toFloat(int $precision = 2): floatprecision to specify the number of decimal places.
Parameters
| Parameter | Type | Description |
|---|---|---|
$precision | int | The number of decimal places to round to. Defaults to 2. |
toCents
Get the currency value represented as an integer in cents. 1 dollar = 100 cents.
public toCents(): intadd
Add one or more amounts or Money objects to the current currency. Parameters here can be either
public add(mixed $args): Moneya numeric value or another Money object. If the parameter is a Money object then the value will be automatically converted using the current exchange rate before it is added.
Parameters
| Parameter | Type | Description |
|---|---|---|
$args | mixed |
subtract
Subtract one or more values or Money objects from the current object. Parameters can be either
public subtract(mixed $args): Moneya numeric value or another Money object. If the parameter is a Money object then the value will be automatically converted using the current exchange rate before it is subtracted.
Parameters
| Parameter | Type | Description |
|---|---|---|
$args | mixed |
set
Sets the value of the Money object.
public set(string $value, ?string $currency): floatParameters
| Parameter | Type | Description |
|---|---|---|
$value | string | the value to set |
$currency | string |
getCurrencyName
Get the name of the currency.
public getCurrencyName(): stringGenerated by Hazaar API Doc Generator on Sun, 01 Mar 2026 09:22:26 +0000