Exchanging currencies

Additional informations on Zend_Currency

Currency informations

Sometimes it is necessary to get informations which are related to a currency. Zend_Currency provides you with several methods to get this informations. Available methods include the following:

  • getCurrencyList(): Returns a list of all currencies which are used in the given region as array. Defaults to the objects locale when no region has been given.

  • getLocale(): Returns the set locale for the actual currency.

  • getName(): Returns the full name for the actual currency. When there is no full name available for the actual currency, it will return the abbreviation for it.

  • getRegionList(): Returns a list of all regions where this currency is used as array. Defaults to the objects currency when no currency has been given.

  • getService(): Returns the set exchange service object for the actual currency.

  • getShortName(): Returns the abbreviation for the actual currency.

  • getSymbol(): Returns the currency sign for the currency. When the currency has no symbol, then it will return the abbreviation for it.

  • getValue(): Returns the set value for the actual currency.

Let's see some code snippets as example:

  1. $currency = new Zend_Currency();
  2.  
  3. var_dump($currency->getValue());
  4. // returns 0
  5.  
  6. var_dump($currency->getRegionList());
  7. // could return an array with all regions where USD is used
  8.  
  9. var_dump($currency->getRegionList('EUR'));
  10. // returns an array with all regions where EUR is used
  11.  
  12. var_dump($currency->getName());
  13. // could return 'US Dollar'
  14.  
  15. var_dump($currency->getName('EUR'));
  16. // returns 'Euro'

As you can see, several methods allow to use additional parameters to override the actual object to get informations for other currencies. Omitting this parameters will return informations from the actual set currency.

Currency Performance Optimization

Zend_Currency's performance can be optimized using Zend_Cache. The static method Zend_Currency::setCache($cache) accepts one option: a Zend_Cache adapter. If the cache adapter is set, the localization data which is used by Zend_Currency will be cached. Additionally there are some static methods for manipulating the cache: getCache(), hasCache(), clearCache() and removeCache().

Example #1 Caching currencies

  1. // creating a cache object
  2. $cache = Zend_Cache::factory('Core',
  3.                              'File',
  4.                              array('lifetime' => 120,
  5.                                    'automatic_serialization' => true),
  6.                              array('cache_dir'
  7.                                        => dirname(__FILE__) . '/_files/'));
  8. Zend_Currency::setCache($cache);

Exchanging currencies