Zend_Measure-Creation.xml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.measure.creation">
  4. <title>Creation of Measurements</title>
  5. <para>
  6. When creating a measurement object, <classname>Zend_Measure_*</classname> methods expect the
  7. input/original measurement data value as the first parameter. This can be a
  8. <link linkend="zend.measure.creation.number">numeric argument</link>, a
  9. <link linkend="zend.measure.creation.string"><type>String</type></link> without units, or a
  10. <link linkend="zend.measure.creation.localized">localized string with unit(s)
  11. specified.</link> The second parameter defines the type of the measurement. Both
  12. parameters are mandatory. The language may optionally be specified as the third parameter.
  13. </para>
  14. <sect2 id="zend.measure.creation.number">
  15. <title>Creating measurements from integers and floats</title>
  16. <para>
  17. In addition to integer data values, floating point types may be used, but
  18. <ulink url="http://www.php.net/float">"simple decimal fractions like 0.1 or 0.7 cannot
  19. be converted into their internal binary counterparts without a little loss of
  20. precision,"</ulink> sometimes giving surprising results. Also, do not compare two
  21. "float" type numbers for equality.
  22. </para>
  23. <example id="zend.measure.creation.number.example-1">
  24. <title>Creation using integer and floating values</title>
  25. <programlisting language="php"><![CDATA[
  26. $measurement = 1234.7;
  27. $unit = new Zend_Measure_Length((integer)$measurement,
  28. Zend_Measure_Length::STANDARD);
  29. echo $unit;
  30. // outputs '1234 m' (meters)
  31. $unit = new Zend_Measure_Length($measurement, Zend_Measure_Length::STANDARD);
  32. echo $unit;
  33. // outputs '1234.7 m' (meters)
  34. ]]></programlisting>
  35. </example>
  36. </sect2>
  37. <sect2 id="zend.measure.creation.string">
  38. <title>Creating measurements from strings</title>
  39. <para>
  40. Many measurements received as input to Zend Framework applications can only be passed
  41. to <classname>Zend_Measure_*</classname> classes as strings, such as numbers written
  42. using <ulink url="http://en.wikipedia.org/wiki/Roman_numerals">roman numerals</ulink>
  43. or extremely large binary values that exceed the precision of <acronym>PHP</acronym>'s
  44. native integer and float types. Since integers can be denoted using strings, if there is
  45. any risk of losing precision due to limitations of <acronym>PHP</acronym>'s native
  46. integer and float types, using strings instead.
  47. <classname>Zend_Measure_Number</classname> uses the BCMath extension to support
  48. arbitrary precision, as shown in the example below, to avoid limitations in many
  49. <acronym>PHP</acronym> functions, such as <ulink
  50. url="http://php.net/bin2dec"><methodname>bin2dec()</methodname></ulink>.
  51. </para>
  52. <example id="zend.measure.creation.string.example-1">
  53. <title>Creation using strings</title>
  54. <programlisting language="php"><![CDATA[
  55. $mystring = "10010100111010111010100001011011101010001";
  56. $unit = new Zend_Measure_Number($mystring, Zend_Measure_Number::BINARY);
  57. echo $unit;
  58. ]]></programlisting>
  59. </example>
  60. </sect2>
  61. <sect2 id="zend.measure.creation.localized">
  62. <title>Measurements from localized strings</title>
  63. <para>
  64. When a string is entered in a localized notation, the correct interpretation can not be
  65. determined without knowing the intended locale. The division of decimal digits with "."
  66. and grouping of thousands with "," is common in the English language, but not so in
  67. other languages. For example, the English number "1,234.50" would be interpreted as
  68. meaning "1.2345" in German. To deal with such problems, the locale-aware
  69. <classname>Zend_Measure_*</classname> family of classes offer the possibility to specify
  70. a language or region to disambiguate the input data and properly interpret the intended
  71. semantic value.
  72. </para>
  73. <example id="zend.measure.creation.localized.example-1">
  74. <title>Localized string</title>
  75. <programlisting language="php"><![CDATA[
  76. $locale = new Zend_Locale('de');
  77. $mystring = "1,234.50";
  78. $unit = new Zend_Measure_Length($mystring,
  79. Zend_Measure_Length::STANDARD,
  80. $locale);
  81. echo $unit; // outputs "1.234 m"
  82. $mystring = "1,234.50";
  83. $unit = new Zend_Measure_Length($mystring,
  84. Zend_Measure_Length::STANDARD,
  85. 'en_US');
  86. echo $unit; // outputs "1234.50 m"
  87. ]]></programlisting>
  88. </example>
  89. <para>
  90. Since Zend Framework 1.7.0 <classname>Zend_Measure</classname> does also support the
  91. usage of an application wide locale. You can simply set a
  92. <classname>Zend_Locale</classname> instance to the registry like shown below. With this
  93. notation you can forget about setting the locale manually with each instance when you
  94. want to use the same locale multiple times.
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. // in your bootstrap file
  98. $locale = new Zend_Locale('de_AT');
  99. Zend_Registry::set('Zend_Locale', $locale);
  100. // somewhere in your application
  101. $length = new Zend_Measure_Length(Zend_Measure_Length::METER();
  102. ]]></programlisting>
  103. </sect2>
  104. </sect1>
  105. <!--
  106. vim:se ts=4 sw=4 et:
  107. -->