You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a PHP library for representing and converting physical units of measure. The utility of this library
6
-
is in encapsulating physical quantities in such a way that you don't have to keep track of which
7
-
unit they're represented in. For instance:
5
+
This is a PHP library for representing and converting physical units of measure. The utility of this library is in encapsulating physical quantities in such a way that you don't have to keep track of which unit they're represented in. For instance:
8
6
9
7
```php
10
8
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
@@ -15,11 +13,10 @@ echo $height->toUnit('m');
15
13
// would print 1.87757, which is 6.16 feet in meters.
16
14
```
17
15
18
-
Having this abstraction allows you to create interfaces that accept physical quantities
19
-
without requiring them to be in a particular unit. For example, this function assumes the
20
-
height is a float of a particular unit (presumably feet):
16
+
Having this abstraction allows you to create interfaces that accept physical quantities without requiring them to be in a particular unit. For example, this function assumes the height is a float of a particular unit (presumably feet):
21
17
22
18
```php
19
+
// Tied to a specific unit of measure
23
20
function isTooTallToRideThisTrain( $height )
24
21
{
25
22
return $height > 5;
@@ -34,6 +31,7 @@ Whereas this version allows for height to be provided in whatever unit is conven
34
31
```php
35
32
use PhpUnitsOfMeasure\PhysicalQuantity\Length;
36
33
34
+
// Free to operate on lengths in any unit of measure
37
35
function isTooTallToRideThisTrain( Length $height )
38
36
{
39
37
return $height->toUnit('ft') > 5;
@@ -44,13 +42,11 @@ isTooTallToRideThisTrain( new Length(2, 'm') );
44
42
```
45
43
46
44
## Installation
47
-
This library is best included in your projects via composer. See the [Composer website](http://getcomposer.org/)
48
-
for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/triplepoint/php-units-of-measure).
45
+
This library is best included in your projects via composer. See the [Composer website](http://getcomposer.org/) for more details, and see the [Packagist.org site for this library](https://packagist.org/packages/triplepoint/php-units-of-measure).
49
46
50
47
## Use
51
48
### Conversion
52
-
As in the examples above, the basic usage of this library is in representing physical quantities and converting
53
-
between typical units of measure. For example:
49
+
As in the examples above, the basic usage of this library is in representing physical quantities and converting between typical units of measure. For example:
54
50
55
51
```php
56
52
$quantity = new \PhpUnitsOfMeasure\PhysicalQuantity\Mass(6, 'lbs');
@@ -64,8 +60,7 @@ echo $quantity; // '6 lbs'
64
60
```
65
61
66
62
### Arithmetic Operators
67
-
There's also support for addition and subtraction. The `PhysicalQuantity` objects are immutable, and as such
68
-
these arithmetic methods return new quantity objects representing the results:
63
+
There's also support for addition and subtraction. The `PhysicalQuantity` objects are immutable, and as such these arithmetic methods return new quantity objects representing the results:
69
64
70
65
```php
71
66
$first = new \PhpUnitsOfMeasure\PhysicalQuantity\Volume(6, 'liters');
@@ -81,10 +76,7 @@ echo $difference; // 4.5804708 l
81
76
### Adding new Units of Measure to Existing Quantities
82
77
Ocassionally, you will need to add a new unit of measure to a pre-existing quantity.
83
78
84
-
For example, let's say in a project you need a new measure of length, called "cubits". You have two
85
-
options: you can permanently add the new unit of measure to the `\PhpUnitsOfMeasure\PhysicalQuantity\Length`
86
-
class (and submit a pull request to get it added upstream, if appropriate), or you can add the
87
-
unit temporarily at run time, inside your calling code.
79
+
For example, let's say in a project you need a new measure of length, called "cubits". You have two options: you can permanently add the new unit of measure to a new child class of the `\PhpUnitsOfMeasure\PhysicalQuantity\Length` class (or add it directly to that class and submit a pull request to get it added upstream, if appropriate), or you can add the unit temporarily at run time, inside your calling code.
88
80
89
81
#### Adding a New Unit of Measure at Runtime
90
82
To add a new unit of measure to an existing quantity at run time, you'd do this:
#### Permanently Adding a New Unit of Measure to a Physical Quantity
129
-
The above method only applies to the specific Length object and is therefore temporary; it would
130
-
be necessary to repeat this process every time you created a new measurement and wanted to use cubits.
121
+
The above method only applies to the specific Length object and is therefore temporary; it would be necessary to repeat this process every time you created a new measurement and wanted to use cubits.
131
122
132
-
A new unit of measure can be permanently added to a physical quantity class by essentially the same process,
133
-
only it would be done inside the constructor of the quantity class. For example:
123
+
A new unit of measure can be permanently added to a physical quantity class by essentially the same process, only it would be done inside the constructor of the quantity class. For example:
134
124
135
125
```php
136
126
namespace PhpUnitsOfMeasure\PhysicalQuantity;
@@ -183,19 +173,12 @@ Now any new object of class `Length` that gets instantiated will come with the c
183
173
### Adding New Physical Quantities
184
174
[Physical quantities](http://en.wikipedia.org/wiki/Physical_quantity) are categories of measurable values, like mass, length, force, etc.
185
175
186
-
For physical quantities that are not already present in this library, it will be necessary to write a class
187
-
to support a new one. All physical quantities extend the `\PhpUnitsOfMeasure\PhysicalQuantity` class, and typically
188
-
have only a constructor method which creates the quantity's units of measure. See above for examples on how to add
189
-
new units to a quantity class.
176
+
For physical quantities that are not already present in this library, it will be necessary to write a class to support a new one. All physical quantities extend the `\PhpUnitsOfMeasure\PhysicalQuantity` class, and typically have only a constructor method which creates the quantity's units of measure. See above for examples on how to add new units to a quantity class.
190
177
191
-
Note that every physical quantity has a chosen "native unit" which is typically the SI standard. The main point for this
192
-
unit is that all of the quantity's other units of measure will convert to and from this chosen native unit. It's
193
-
important to be aware of a quantity's native unit when writing conversions for new units of measure.
178
+
Note that every physical quantity has a chosen "native unit" which is typically the SI standard. The main point for this unit is that all of the quantity's other units of measure will convert to and from this chosen native unit. It's important to be aware of a quantity's native unit when writing conversions for new units of measure.
194
179
195
180
### Adding new Aliases to Existing Units
196
-
It may come up that the right unit of measure exists for the right physical quantity, but there's a missing
197
-
alias for the unit. For example, if you thought 'footses' was an obviously lacking alias for the Length unit 'ft', you could
198
-
temporarily add the alias like this:
181
+
It may come up that the right unit of measure exists for the right physical quantity, but there's a missing alias for the unit. For example, if you thought 'footses' was an obviously lacking alias for the Length unit 'ft', you could temporarily add the alias like this:
199
182
200
183
```php
201
184
use \PhpUnitsOfMeasure\PhysicalQuantity\Length;
@@ -220,12 +203,9 @@ API documentation (such as it is) is handled through GitApiDoc.
Pull requests are welcome, especially regarding new units of measure or new physical quantities. However, please note that there
224
-
are many sources for conversion factors, and not all are careful to respect known precision.
206
+
Pull requests are welcome, especially regarding new units of measure or new physical quantities. However, please note that there are many sources for conversion factors, and not all are careful to respect known precision.
225
207
226
-
In the United States, the standards body for measurement is NIST, and they've published [NIST Special Publication 1038 "The International
227
-
System of Units (SI) - Conversion factors for General Use"](http://www.nist.gov/pml/wmd/metric/upload/SP1038.pdf). This guide
228
-
contains the approved conversion factors between various units and the base SI units.
208
+
In the United States, the standards body for measurement is NIST, and they've published [NIST Special Publication 1038 "The International System of Units (SI) - Conversion factors for General Use"](http://www.nist.gov/pml/wmd/metric/upload/SP1038.pdf). This guide contains the approved conversion factors between various units and the base SI units.
229
209
230
210
Also note that any new physical quantities should have the appropriate SI unit chosen for their native unit of measure.
231
211
@@ -258,3 +238,5 @@ Codesniffer verifies that coding standards are being met. Once the project is b
0 commit comments