Kapitel 18. Zend_Measure

Inhaltsverzeichnis

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. Compare
18.4.5. Manually change values
18.4.6. Manually change types
18.5. Special functions
18.5.1. Listing all known types
18.5.2. Listing all known units
18.6. Types of measurements

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.

Beispiel 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/Measure/Length.php';

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

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

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.