Introducción a Zend Framework

 Aprendiendo Zend Framework


 Referencia de Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

    25.9. Exchanging currencies

    Within the previous section we discussed currency calculations. But as you can imaging calculating currencies does often mean to calculate different currencies from different countries.

    In this case the currencies have to be exchanged so that both use the same currency. Within real live this information is available by banks or by daily papers. But as we are in web, we should use available exchange services. Zend_Currency allows their usage with a simple callback.

    First let's write a simple exchange service.

    class SimpleExchange implements Zend_Currency_CurrencyInterface
        public function 
            if (
    $from !== "USD") {
                throw new 
    Exception('We can only exchange USD');

            switch (
    $to) {

           throw new 
    Exception('Unable to exchange $to');

    We have now created a manual exchange service. It will not fit the real live, but it shows you how currency exchange works.

    Your exchange class must implement the Zend_Currency_CurrencyInterface interface. This interface requires the single method getRate() to be implemented. This method has two parameters it will receive. Both are the short names for the given currencies. Zend_Currency on the other side needs the exchange rate to be returned.

    In a living exchange class you would probably ask the service provider for the correct exchange rates. For our example the manual rate will be ok.

    Now we will simply attach our exchange class with Zend_Currency. There are two ways to do this. Eigher by attaching a instance of the Exchange class, or by simply giving a string with the classname.

    $currency = new Zend_Currency(
    'value'    => 1000,
    'currency' => 'EUR',

    $service  = new SimpleExchange();

    // attach the exchange service

    $currency2 = new Zend_Currency(
    'value'    => 1000,
    'currency' => 'USD',


    The above example will return '$ 3.000' because the 1.000 USD will be converted by a rate of 2 to 2.000 EUR.

    [Nota] Calculation without exchange service

    When you try to calculate two currency objects which do not use the same currency and have no exchange service attached, you will get an exception. The reason is that Zend_Currency is then not able to switch between the different currencies.

    digg delicious meneame google twitter technorati facebook