PHP code example of mgamadeus / ddd-common-money

1. Go to this page and download the library: Download mgamadeus/ddd-common-money library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mgamadeus / ddd-common-money example snippets


use DDD\Domain\Common\Entities\Money\Currency;
use DDD\Domain\Common\Entities\Money\MoneyAmount;

// Create a monetary amount
$price = new MoneyAmount(29.99, 'EUR');
$price = new MoneyAmount(0.0, Currency::byIsoCode('USD'));

// Use as entity property (stored as JSON)
class Product extends Entity
{
    public ?MoneyAmount $price = null;
}

// Compare (checks both amount AND currency)
$a = new MoneyAmount(10.0, 'EUR');
$b = new MoneyAmount(10.0, 'USD');
$a->isEqualTo($b);  // false -- different currency

namespace App\Domain\Common\Entities\Money;

use DDD\Domain\Common\Entities\Money\Currency as BaseCurrency;

class Currency extends BaseCurrency
{
    // Add custom currencies or symbols
}