Capítulo 18. Zend_Measure

Índice

18.1. Introduction
18.2. Creation of Measurements
18.2.1. Measurements from variables
18.2.2. Measurements from strings
18.2.3. Measurements from localized strings
18.3. Outputting measurements
18.3.1. Automatic output
18.3.2. Outputting values
18.3.3. Output with unit of measurement
18.3.4. Output as localized string
18.4. Manipulating measurements
18.4.1. Convert
18.4.2. Add and subtract
18.4.3. Compare
18.4.4. Calculate differences
18.4.5. Manually change values
18.4.6. Manually change types
18.5. Special functions
18.5.1. Serializing and Deserializing
18.5.2. Listing all known types
18.5.3. Listing all known units
18.6. Types of measurements
18.6.1. Zend_Measure_Acceleration
18.6.2. Zend_Measure_Angle
18.6.3. Zend_Measure_Area
18.6.4. Zend_Measure_Binary
18.6.5. Zend_Measure_Capacitance
18.6.6. Zend_Measure_Cooking_Volume
18.6.7. Zend_Measure_Cooking_Weight
18.6.8. Zend_Measure_Current
18.6.9. Zend_Measure_Density
18.6.10. Zend_Measure_Energy
18.6.11. Zend_Measure_Force
18.6.12. Zend_Measure_Flow_Mass
18.6.13. Zend_Measure_Flow_Mole
18.6.14. Zend_Measure_Flow_Volume
18.6.15. Zend_Measure_Frequency
18.6.16. Zend_Measure_Illumination
18.6.17. Zend_Measure_Length
18.6.18. Zend_Measure_Lightness
18.6.19. Zend_Measure_Number
18.6.20. Zend_Measure_Power
18.6.21. Zend_Measure_Pressure
18.6.22. Zend_Measure_Speed
18.6.23. Zend_Measure_Temperature
18.6.24. Zend_Measure_Torque
18.6.25. Zend_Measure_Viscosity_Dynamic
18.6.26. Zend_Measure_Viscosity_Kinematic
18.6.27. Zend_Measure_Volume
18.6.28. Zend_Measure_Weight

18.1. Introduction

Zend_Measure provides a generic and very easy way for working with measurements.

Using Zend_Measure, you can convert measurements into different units of the same type. They can be added, subtracted and compared against each other. From a given input made in the user's native language, the unit of measurement can be automatically extracted.

Of course a great number of completely different units of measurement is being supported. Most of these units are of a physical nature, but there are also special measurements used, in numerics or other areas, that are only partly physical - or not at all.

The following quickstart tutorial shows how units of measurement can be automatically converted.

Exemplo 18.1. Converting measurements

To convert a measurement, its value and its type have to be known. The value can be an integer, a float, or even a string.

<?php
require_once 'Zend.php';
Zend::loadClass('Zend_Measure');

// The Length class is needed when using the constants
Zend::loadClass('Zend_Measure_Length');

$locale = new Zend_Locale('en');
$unit = new Zend_Measure(100,Zend_Measure_Length::METER, $locale);

// Convert meters to yards
echo $unit->convertTo(Zend_Measure_Length::YARD);
?>
[Nota] Nota

As you can see, the example consists of only 4 lines of code. The usage of Zend_Measure is so compact that usually 4 lines of code is all that's needed.