Chapter 16. Zend_Measure

Table of Contents

16.1. Introduction
16.2. Measurements in the Zend Framework
16.3. Creation of Measurements
16.3.1. Measurements from variables
16.3.2. Measurements from strings
16.3.3. Measurements from localized strings
16.4. Outputting measurements
16.4.1. Automatic output
16.4.2. Outputting values
16.4.3. Output with unit of measurement
16.4.4. Output as localized string
16.5. Manipulating measurements
16.5.1. Convert
16.5.2. Add and subtract
16.5.3. Compare
16.5.4. Calculate differences
16.5.5. Manually change values
16.5.6. Manually change types
16.6. Special functions
16.6.1. Serializing and Deserializing
16.6.2. Listing all known types
16.6.3. Listing all known units
16.7. Types of measurements
16.7.1. Zend_Measure_Acceleration
16.7.2. Zend_Measure_Angle
16.7.3. Zend_Measure_Area
16.7.4. Zend_Measure_Binary
16.7.5. Zend_Measure_Capacitance
16.7.6. Zend_Measure_Cooking_Volume
16.7.7. Zend_Measure_Cooking_Weight
16.7.8. Zend_Measure_Current
16.7.9. Zend_Measure_Density
16.7.10. Zend_Measure_Energy
16.7.11. Zend_Measure_Force
16.7.12. Zend_Measure_Flow_Mass
16.7.13. Zend_Measure_Flow_Mole
16.7.14. Zend_Measure_Flow_Volume
16.7.15. Zend_Measure_Frequency
16.7.16. Zend_Measure_Illumination
16.7.17. Zend_Measure_Length
16.7.18. Zend_Measure_Lightness
16.7.19. Zend_Measure_Number
16.7.20. Zend_Measure_Power
16.7.21. Zend_Measure_Pressure
16.7.22. Zend_Measure_Speed
16.7.23. Zend_Measure_Temperature
16.7.24. Zend_Measure_Torque
16.7.25. Zend_Measure_Viscosity_Dynamic
16.7.26. Zend_Measure_Viscosity_Kinematic
16.7.27. Zend_Measure_Volume
16.7.28. Zend_Measure_Weight

16.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.

Example 16.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);
?>
[Note] Note

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.